{"id":4968,"date":"2019-02-20T15:36:36","date_gmt":"2019-02-20T23:36:36","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/paul\/?p=4968"},"modified":"2019-03-12T17:05:56","modified_gmt":"2019-03-13T00:05:56","slug":"the-curious-case-of-copying-filestream-files-between-tables","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/","title":{"rendered":"The Curious Case of&#8230; copying FILESTREAM files between tables"},"content":{"rendered":"<p style=\"text-align: justify;\"><em>(The Curious Case of&#8230;<\/em> used to be part of our bi-weekly <a href=\"https:\/\/www.sqlskills.com\/join-the-sqlskills-insider-community\/\" target=\"_blank\" rel=\"noopener noreferrer\">newsletter<\/a>\u00a0but we decided to make it a\u00a0regular blog post instead so it can sometimes be more frequent. It covers something interesting one of us encountered when working with a client, doing some testing, or were asked in a random question from the community.)<\/p>\n<p style=\"text-align: justify;\">I was asked last week whether it&#8217;s possible to create a table with a <em>FILESTREAM<\/em> column and then populate that column by copying <em>FILESTREAM<\/em> files from another directory in the <em>FILESTREAM<\/em> data container.<\/p>\n<p style=\"text-align: justify;\">The simple answer is no &#8211; you can&#8217;t use that method for copying <em>FILESTREAM<\/em> files between tables.<\/p>\n<p style=\"text-align: justify;\">In the data container, each table with a <em>FILESTREAM<\/em> column (or partition of a partitioned table with a <em>FILESTREAM<\/em> column) has a directory with a GUID name, and then there&#8217;s a sub-directory for each <em>FILESTREAM<\/em> column that also have GUID names. Then the <em>FILESTREAM<\/em> files themselves have filenames in the format <em>8hexdigits-8hexdigits-4hexdigits<\/em>. The hex digits are the Log Sequence Number (LSN) of the log record that created the <em>FILESTREAM<\/em> file, and the data record contains that\u00a0LSN as part of the in-row portion of the <em>FILESTREAM<\/em> column. (You can read a full explanation of the storage format in my post <a href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/filestream-directory-structure\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em>FILESTREAM directory structure<\/em><\/a>.)<\/p>\n<p style=\"text-align: justify;\">So, simply copying the files from one directory to another wouldn&#8217;t work for two reasons:<\/p>\n<ul style=\"text-align: justify;\">\n<li>The <em>FILESTREAM<\/em> column in the data record of the target table wouldn&#8217;t be populated<\/li>\n<li>And even if you created empty <em>FILESTREAM<\/em> columns, the LSN in the column wouldn&#8217;t match the LSN of the copied-in files<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">And you can&#8217;t use <em>SELECT-INTO<\/em> either, because it will not create <em>FILESTREAM<\/em> files and instead copies them and stores them in the new table as <em>varbinary(max)<\/em> with a limit of 2GB.<\/p>\n<p style=\"text-align: justify;\"><strong>Bottom line:<\/strong>\u00a0You can use <em>INSERT INTO &lt;target table&gt; SELECT * FROM &lt;source table&gt;<\/em> and that *will* populate the <em>FILESTREAM<\/em> files, but that means reading the <em>FILESTREAM<\/em> data through SQL Server&#8217;s buffer pool, which may be very inefficient for large amounts of data. So your best bet may be to write a little application that will using NTFS streaming access to directly copy the <em>FILESTREAM<\/em> data from the source table to the target table.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(The Curious Case of&#8230; used to be part of our bi-weekly newsletter\u00a0but we decided to make it a\u00a0regular blog post instead so it can sometimes be more frequent. It covers something interesting one of us encountered when working with a client, doing some testing, or were asked in a random question from the community.) I [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,115],"tags":[],"class_list":["post-4968","post","type-post","status-publish","format-standard","hentry","category-filestream","category-the-curious-case-of"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Copying FILESTREAM files between tables in SQL Server<\/title>\n<meta name=\"description\" content=\"What&#039;s the best way of copying FILESTREAM files between tables?\" \/>\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\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Copying FILESTREAM files between tables in SQL Server\" \/>\n<meta property=\"og:description\" content=\"What&#039;s the best way of copying FILESTREAM files between tables?\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-20T23:36:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-13T00:05:56+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\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\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/\",\"name\":\"Copying FILESTREAM files between tables in SQL Server\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2019-02-20T23:36:36+00:00\",\"dateModified\":\"2019-03-13T00:05:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"description\":\"What's the best way of copying FILESTREAM files between tables?\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The Curious Case of&#8230; copying FILESTREAM files between tables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Copying FILESTREAM files between tables in SQL Server","description":"What's the best way of copying FILESTREAM files between tables?","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\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/","og_locale":"en_US","og_type":"article","og_title":"Copying FILESTREAM files between tables in SQL Server","og_description":"What's the best way of copying FILESTREAM files between tables?","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/","og_site_name":"Paul S. Randal","article_published_time":"2019-02-20T23:36:36+00:00","article_modified_time":"2019-03-13T00:05:56+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/","name":"Copying FILESTREAM files between tables in SQL Server","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2019-02-20T23:36:36+00:00","dateModified":"2019-03-13T00:05:56+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"description":"What's the best way of copying FILESTREAM files between tables?","breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/the-curious-case-of-copying-filestream-files-between-tables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"The Curious Case of&#8230; copying FILESTREAM files between tables"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/4968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=4968"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/4968\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=4968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=4968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=4968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}