I just ran into this question on StackOverflow: Dump data into single XML file from MS SQL Server 2008 R2.
In there Daniel is looking for a solution to export his database as an XML file.
I thought that request was an excellent way to practice my XML skills so I spend a little time to come up with a solution.
For testing purposes I first created a few tables in an empty database:
DECLARE @cmd NVARCHAR(MAX) ;
SET @cmd = ( SELECT 'CREATE TABLE T' + CAST(n AS NVARCHAR(MAX))
+ '(id INT IDENTITY(1,1)'
+ (SELECT ',c' + CAST(c.n AS NVARCHAR(MAX))
+ ' INT DEFAULT ' + CAST(c.n AS NVARCHAR(MAX))
+ '*' + CAST(t.n AS NVARCHAR(MAX))
FROM dbo.GetNums(( t.n - 1 ) % 10 + 1) c
FOR XML PATH('') ,
TYPE).value('.', 'NVARCHAR(MAX)') + ');INSERT INTO T'
+ CAST(n AS NVARCHAR(MAX)) + ' DEFAULT VALUES;'
FROM dbo.GetNums (3000) t
FOR XML PATH('') ,
TYPE).value('.', 'NVARCHAR(MAX)') ;
EXEC(@cmd) ;
[/sql]
This snippet uses Itzik's GetNums function to generate 3000 CREATE TABLE and INSERT statements.
With the table in place I was able to come up with this solution to the original question:
EXEC(@cmd) ;
[/sql]
The script builds a single statement. For each table it selects the table name in a column with the name [@name] and the content of that table as XML (using a sub-select) in a column with the name data. All these SELECTs are "UNION ALL"ed together and formated into the final XML document with an additional outer SELECT FOR XML.
If you are only after some of the tables, you can add a where clause in row 8 of the above snippet.
The resulting XML has this format:
For each table there is a <table> tag that has a name attribut with the table name. It contains a <data> tag that in turn contains one <row> tag for every row in the table.
The only thing missing to make this valid XML is a root tag, but that should be easy to add.
3 Responses to Selecting the entire Database as XML String