{"id":477,"date":"2012-07-12T11:41:00","date_gmt":"2012-07-12T11:41:00","guid":{"rendered":"\/blogs\/bobb\/post\/Saving-the-contents-of-a-SQL-Server-XML-column-to-a-file.aspx"},"modified":"2013-01-03T23:59:20","modified_gmt":"2013-01-04T07:59:20","slug":"saving-the-contents-of-a-sql-server-xml-column-to-a-file","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/","title":{"rendered":"Saving the contents of a SQL Server XML column to a file"},"content":{"rendered":"<p>\nWhile preparing for my TechEd talk on filetables and full-text search, I came across this tidbit that I thought was interesting.\n<\/p>\n<p>\nWith the advent of XML support in databases, specifically SQL Server&#39;s XML data type in SQL Server 2005, there has been two main ways to store XML; in files on the file system, and in a database. SQL Server has a built-in way to move XML data from files to the database, namely the OLE DB BULK provider and OPENROWSET SINGLE BLOB\/CLOB\/NCLOB. It looks like this\n<\/p>\n<p>\n&#8212; Load XML from a file<br \/>\nDECLARE @x XML;<br \/>\nSET @x = ( SELECT * FROM OPENROWSET( BULK &#39;C:\\invoice1.xml&#39;, SINGLE_BLOB) AS x);\n<\/p>\n<p>\n&#8212; Or direct to table<br \/>\nINSERT INTO dbo.invoice_docs(invoice)<br \/>\nSELECT * FROM OPENROWSET(BULK &#39;C:\\invoice1.xml&#39;, SINGLE_BLOB) AS x;<br \/>\ngo\n<\/p>\n<p>\nBecause there is no corresponding built-in way to move XML from the database to files, I&#39;d always referred to this asymmetry as &quot;all your XML belong to us&quot;. Or, a bit more rudely, as &quot;SQL Server is the roach motel of XML files, they get in, but they can&#39;t get out&quot;.\n<\/p>\n<p>\nAll joking aside, in SQL Server 2012, filetables provide a straightforward way to export XML column data (or any data for that matter) as files using only T-SQL. Suppose I have an have a filetable named Documents. This code stores the XML resume from the row with JobCandidateID of 1 to a file in the root directory of the filetable named &quot;JobCandidate1.xml&quot;. To store this into a subdirectory using T-SQL, see the code in <a href=\"http:\/\/3.209.169.194\/blogs\/bobb\/sql-server-2012-filetables-in-t-sql-part-2-new-rows\/\">this blog entry<\/a>. You can then copy the file to the file system location of your choice.\n<\/p>\n<p>\n&#8212; get existing database blob into files<br \/>\ndeclare @resume varbinary(max);<br \/>\ndeclare @name varchar(40);\n<\/p>\n<p>\nselect @resume = cast(Resume as varbinary(max))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,@name = &#39;JobCandidate&#39; + cast(JobCandidateID as varchar(1)) + &#39;.xml&#39;<br \/>\nfrom AdventureWorks.HumanResources.JobCandidate<br \/>\nwhere JobCandidateID = 1;\n<\/p>\n<p>\ninsert dbo.Documents(Name, file_stream) values(@name, @resume);\n<\/p>\n<p>\nNow XML files can get out!\n<\/p>\n<p>\n@bobbeauch<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While preparing for my TechEd talk on filetables and full-text search, I came across this tidbit that I thought was interesting. With the advent of XML support in databases, specifically SQL Server&#39;s XML data type in SQL Server 2005, there has been two main ways to store XML; in files on the file system, and [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,31,37],"tags":[],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-filetable","category-sql-server-2012","category-sql-server-xml"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Saving the contents of a SQL Server XML column to a file - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Saving the contents of a SQL Server XML column to a file - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"While preparing for my TechEd talk on filetables and full-text search, I came across this tidbit that I thought was interesting. With the advent of XML support in databases, specifically SQL Server&#039;s XML data type in SQL Server 2005, there has been two main ways to store XML; in files on the file system, and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2012-07-12T11:41:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-04T07:59:20+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/\",\"name\":\"Saving the contents of a SQL Server XML column to a file - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2012-07-12T11:41:00+00:00\",\"dateModified\":\"2013-01-04T07:59:20+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"FileTable\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/filetable\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Saving the contents of a SQL Server XML column to a file\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Saving the contents of a SQL Server XML column to a file - Bob Beauchemin","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:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/","og_locale":"en_US","og_type":"article","og_title":"Saving the contents of a SQL Server XML column to a file - Bob Beauchemin","og_description":"While preparing for my TechEd talk on filetables and full-text search, I came across this tidbit that I thought was interesting. With the advent of XML support in databases, specifically SQL Server&#39;s XML data type in SQL Server 2005, there has been two main ways to store XML; in files on the file system, and [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/","og_site_name":"Bob Beauchemin","article_published_time":"2012-07-12T11:41:00+00:00","article_modified_time":"2013-01-04T07:59:20+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/","name":"Saving the contents of a SQL Server XML column to a file - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2012-07-12T11:41:00+00:00","dateModified":"2013-01-04T07:59:20+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/saving-the-contents-of-a-sql-server-xml-column-to-a-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"FileTable","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/filetable\/"},{"@type":"ListItem","position":3,"name":"Saving the contents of a SQL Server XML column to a file"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=477"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/477\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}