Thursday, May 8, 2008

Convering SQL resultset to XML file

Hello All,
Here's the simplest way to convert SQL result to XML output
-- declare an XML variable
DECLARE @x XML
SET @x = ''

-- create another XML variable
DECLARE @t XML
SELECT @t = ( SELECT TOP 3 name FROM sys.tables FOR XML AUTO)

-- insert the second XML variable to the first one
SET @x.modify( '
insert sql:variable("@t")
as last into (/Root)[1] ' )

-- Let us check the results
SELECT @x

/*





*/

No comments: