{"id":577,"date":"2012-01-13T12:09:15","date_gmt":"2012-01-13T17:09:15","guid":{"rendered":"http:\/\/sqlity.net\/en\/?p=577"},"modified":"2014-11-13T14:02:26","modified_gmt":"2014-11-13T19:02:26","slug":"selecting-the-entire-database-as-xml-string","status":"publish","type":"post","link":"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/","title":{"rendered":"Selecting the entire Database as XML String"},"content":{"rendered":"<p>\nI just ran into this question on StackOverflow: <a href=\"http:\/\/stackoverflow.com\/questions\/8851638\/dump-data-into-single-xml-file-from-ms-sql-server-2008-r2\">Dump data into single XML file from MS SQL Server 2008 R2<\/a>.<br \/>\nIn there Daniel is looking for a solution to export his database as an XML file.\n<\/p>\n<p>\nI thought that request was an excellent way to practice my XML skills so I spend a little time to come up with a solution.\n<\/p>\n<p>\nFor testing purposes I first created a few tables in an empty database:\n<\/p>\n<div>\n[sql]\n<p>DECLARE @cmd NVARCHAR(MAX) ;<br \/>\nSET @cmd = ( SELECT 'CREATE TABLE T' + CAST(n AS NVARCHAR(MAX))<br \/>\n                    + '(id INT IDENTITY(1,1)'<br \/>\n                    + (SELECT   ',c' + CAST(c.n AS NVARCHAR(MAX))<br \/>\n                                + ' INT DEFAULT ' + CAST(c.n AS NVARCHAR(MAX))<br \/>\n                                + '*' + CAST(t.n AS NVARCHAR(MAX))<br \/>\n                       FROM     dbo.GetNums(( t.n - 1 ) % 10 + 1) c<br \/>\n             FOR    XML PATH('') ,<br \/>\n                        TYPE).value('.', 'NVARCHAR(MAX)') + ');INSERT INTO T'<br \/>\n                    + CAST(n AS NVARCHAR(MAX)) + ' DEFAULT VALUES;'<br \/>\n             FROM   dbo.GetNums (3000) t<br \/>\nFOR XML PATH('') ,<br \/>\n        TYPE).value('.', 'NVARCHAR(MAX)') ;<\/p>\n<p>EXEC(@cmd) ;<br \/>\n[\/sql]\n<\/p><\/div>\n<p>\nThis snippet uses <a href=\"http:\/\/www.sqlmag.com\/article\/sql-server\/virtual-auxiliary-table-of-numbers\">Itzik's GetNums<\/a> function to generate 3000 CREATE TABLE and INSERT statements.\n<\/p>\n<p>\nWith the table in place I was able to come up with this solution to the original question:\n<\/p>\n<div>\n[sql]\nDECLARE @cmd NVARCHAR(MAX) ;<br \/>\nSET @cmd = 'SELECT * FROM('<br \/>\n    + STUFF(<br \/>\n(SELECT ' UNION ALL SELECT ''' + QUOTENAME(OBJECT_SCHEMA_NAME(object_id))<br \/>\n        + '.' + QUOTENAME(name) + ''' [@name],' + '(SELECT * FROM '<br \/>\n        + QUOTENAME(OBJECT_SCHEMA_NAME(object_id)) + '.' + QUOTENAME(name)<br \/>\n        + ' FOR XML PATH(''row''),TYPE) data'<br \/>\n FROM   sys.tables<br \/>\n    FOR     XML PATH('') ,<br \/>\n                TYPE).value('.', 'NVARCHAR(MAX)'), 1, 11, '')<br \/>\n    + ')X FOR XML PATH(''table''),TYPE;' ;<\/p>\n<p>EXEC(@cmd) ;<br \/>\n[\/sql]\n<\/p><\/div>\n<p>\nThe 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.\n<\/p>\n<p>\nIf you are only after some of the tables, you can add a where clause in row 8 of the above snippet.\n<\/p>\n<p>\nThe resulting XML has this format:\n<\/p>\n<div>\n[xml]\n&amp;lt;table name=&amp;quot;[dbo].[T1628]&amp;quot;&amp;gt;<br \/>\n  &amp;lt;data&amp;gt;<br \/>\n    &amp;lt;row&amp;gt;<br \/>\n      &amp;lt;id&amp;gt;1&amp;lt;\/id&amp;gt;<br \/>\n      &amp;lt;c1&amp;gt;1628&amp;lt;\/c1&amp;gt;<br \/>\n      &amp;lt;c2&amp;gt;3256&amp;lt;\/c2&amp;gt;<br \/>\n      &amp;lt;c3&amp;gt;4884&amp;lt;\/c3&amp;gt;<br \/>\n      &amp;lt;c4&amp;gt;6512&amp;lt;\/c4&amp;gt;<br \/>\n      &amp;lt;c5&amp;gt;8140&amp;lt;\/c5&amp;gt;<br \/>\n      &amp;lt;c6&amp;gt;9768&amp;lt;\/c6&amp;gt;<br \/>\n      &amp;lt;c7&amp;gt;11396&amp;lt;\/c7&amp;gt;<br \/>\n      &amp;lt;c8&amp;gt;13024&amp;lt;\/c8&amp;gt;<br \/>\n    &amp;lt;\/row&amp;gt;<br \/>\n  &amp;lt;\/data&amp;gt;<br \/>\n&amp;lt;\/table&amp;gt;<br \/>\n&amp;lt;table name=&amp;quot;[dbo].[T2102]&amp;quot;&amp;gt;<br \/>\n  &amp;lt;data&amp;gt;<br \/>\n    &amp;lt;row&amp;gt;<br \/>\n      &amp;lt;id&amp;gt;1&amp;lt;\/id&amp;gt;<br \/>\n      &amp;lt;c1&amp;gt;2102&amp;lt;\/c1&amp;gt;<br \/>\n      &amp;lt;c2&amp;gt;4204&amp;lt;\/c2&amp;gt;<br \/>\n    &amp;lt;\/row&amp;gt;<br \/>\n  &amp;lt;\/data&amp;gt;<br \/>\n&amp;lt;\/table&amp;gt;<br \/>\n[\/xml]\n<\/div>\n<p>\nFor each table there is a &lt;table&gt; tag that has a name attribut with the table name. It contains a &lt;data&gt; tag that in turn contains one &lt;row&gt; tag for every row in the table.\n<\/p>\n<p>\nThe only thing missing to make this valid XML is a root tag, but that should be easy to add.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 <a href=\"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/\">[more&#8230;]<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5,16],"tags":[15,271],"class_list":["post-577","post","type-post","status-publish","format-standard","hentry","category-general","category-xml","tag-sql-server","tag-xml"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Selecting the entire Database as XML String<\/title>\n<meta name=\"description\" content=\"This article describes how to convert your entire database content into a single xml document.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Selecting the entire Database as XML String\" \/>\n<meta property=\"og:description\" content=\"This article describes how to convert your entire database content into a single xml document.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/\" \/>\n<meta property=\"og:site_name\" content=\"sqlity.net\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/sqlity.net\" \/>\n<meta property=\"article:published_time\" content=\"2012-01-13T17:09:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-11-13T19:02:26+00:00\" \/>\n<meta name=\"author\" content=\"Sebastian Meine\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@sqlity\" \/>\n<meta name=\"twitter:site\" content=\"@sqlity\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sebastian Meine\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/577\\\/selecting-the-entire-database-as-xml-string\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/577\\\/selecting-the-entire-database-as-xml-string\\\/\"},\"author\":{\"name\":\"Sebastian Meine\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#\\\/schema\\\/person\\\/bcffd8c572bc2f1bd10fdba80135e53c\"},\"headline\":\"Selecting the entire Database as XML String\",\"datePublished\":\"2012-01-13T17:09:15+00:00\",\"dateModified\":\"2014-11-13T19:02:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/577\\\/selecting-the-entire-database-as-xml-string\\\/\"},\"wordCount\":630,\"commentCount\":3,\"keywords\":[\"SQL Server\",\"XML\"],\"articleSection\":[\"General\",\"XML\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/sqlity.net\\\/en\\\/577\\\/selecting-the-entire-database-as-xml-string\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/577\\\/selecting-the-entire-database-as-xml-string\\\/\",\"url\":\"https:\\\/\\\/sqlity.net\\\/en\\\/577\\\/selecting-the-entire-database-as-xml-string\\\/\",\"name\":\"Selecting the entire Database as XML String\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#website\"},\"datePublished\":\"2012-01-13T17:09:15+00:00\",\"dateModified\":\"2014-11-13T19:02:26+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#\\\/schema\\\/person\\\/bcffd8c572bc2f1bd10fdba80135e53c\"},\"description\":\"This article describes how to convert your entire database content into a single xml document.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/577\\\/selecting-the-entire-database-as-xml-string\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/sqlity.net\\\/en\\\/577\\\/selecting-the-entire-database-as-xml-string\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/577\\\/selecting-the-entire-database-as-xml-string\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/sqlity.net\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Selecting the entire Database as XML String\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/sqlity.net\\\/en\\\/\",\"name\":\"sqlity.net\",\"description\":\"Quality for SQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/sqlity.net\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#\\\/schema\\\/person\\\/bcffd8c572bc2f1bd10fdba80135e53c\",\"name\":\"Sebastian Meine\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g\",\"caption\":\"Sebastian Meine\"},\"sameAs\":[\"http:\\\/\\\/sqlity.net\",\"https:\\\/\\\/x.com\\\/sqlity\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Selecting the entire Database as XML String","description":"This article describes how to convert your entire database content into a single xml document.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/","og_locale":"en_US","og_type":"article","og_title":"Selecting the entire Database as XML String","og_description":"This article describes how to convert your entire database content into a single xml document.","og_url":"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/","og_site_name":"sqlity.net","article_publisher":"https:\/\/www.facebook.com\/sqlity.net","article_published_time":"2012-01-13T17:09:15+00:00","article_modified_time":"2014-11-13T19:02:26+00:00","author":"Sebastian Meine","twitter_card":"summary_large_image","twitter_creator":"@sqlity","twitter_site":"@sqlity","twitter_misc":{"Written by":"Sebastian Meine","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/#article","isPartOf":{"@id":"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/"},"author":{"name":"Sebastian Meine","@id":"https:\/\/sqlity.net\/en\/#\/schema\/person\/bcffd8c572bc2f1bd10fdba80135e53c"},"headline":"Selecting the entire Database as XML String","datePublished":"2012-01-13T17:09:15+00:00","dateModified":"2014-11-13T19:02:26+00:00","mainEntityOfPage":{"@id":"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/"},"wordCount":630,"commentCount":3,"keywords":["SQL Server","XML"],"articleSection":["General","XML"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/","url":"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/","name":"Selecting the entire Database as XML String","isPartOf":{"@id":"https:\/\/sqlity.net\/en\/#website"},"datePublished":"2012-01-13T17:09:15+00:00","dateModified":"2014-11-13T19:02:26+00:00","author":{"@id":"https:\/\/sqlity.net\/en\/#\/schema\/person\/bcffd8c572bc2f1bd10fdba80135e53c"},"description":"This article describes how to convert your entire database content into a single xml document.","breadcrumb":{"@id":"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/sqlity.net\/en\/577\/selecting-the-entire-database-as-xml-string\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/sqlity.net\/en\/"},{"@type":"ListItem","position":2,"name":"Selecting the entire Database as XML String"}]},{"@type":"WebSite","@id":"https:\/\/sqlity.net\/en\/#website","url":"https:\/\/sqlity.net\/en\/","name":"sqlity.net","description":"Quality for SQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/sqlity.net\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/sqlity.net\/en\/#\/schema\/person\/bcffd8c572bc2f1bd10fdba80135e53c","name":"Sebastian Meine","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g","caption":"Sebastian Meine"},"sameAs":["http:\/\/sqlity.net","https:\/\/x.com\/sqlity"]}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2wXuw-9j","jetpack-related-posts":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/posts\/577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/comments?post=577"}],"version-history":[{"count":0,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/posts\/577\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/media?parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/categories?post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/tags?post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}