Skip to content

Instantly share code, notes, and snippets.

@AsifMushtaq
Created February 12, 2014 10:18
Show Gist options
  • Save AsifMushtaq/8952950 to your computer and use it in GitHub Desktop.
Save AsifMushtaq/8952950 to your computer and use it in GitHub Desktop.
SQL SERVER – Simple Example of Creating XML File Using T-SQL
SELECT (
SELECT 'White' AS Color1,
'Blue' AS Color2,
'Black' AS Color3,
'Light' AS 'Color4/@Special',
'Green' AS Color4,
'Red' AS Color5
FOR
XML PATH('Colors'),
TYPE
),
(
SELECT 'Apple' AS Fruits1,
'Pineapple' AS Fruits2,
'Grapes' AS Fruits3,
'Melon' AS Fruits4
FOR
XML PATH('Fruits'),
TYPE
)
FOR
XML PATH(''),
ROOT('SampleXML')
GO
/* This would generated the following xml.
<SampleXML>
<Colors>
<Color1>White</Color1>
<Color2>Blue</Color2>
<Color3>Black</Color3>
<Color4 Special="Light">Green</Color4>
<Color5>Red</Color5>
</Colors>
<Fruits>
<Fruits1>Apple</Fruits1>
<Fruits2>Pineapple</Fruits2>
<Fruits3>Grapes</Fruits3>
<Fruits4>Melon</Fruits4>
</Fruits>
</SampleXML>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment