Sql-docs: Provide examples for insert of folders/files via T-SQL

Created on 16 Jun 2020  ·  7Comments  ·  Source: MicrosoftDocs/sql-docs

Please provide examples on how to craft T-SQL statements to create folders or insert files.
How can I insert a file using T-SQL into specific folder? There is no example on how this can be done.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri2 assigned-to-author doc-bug filestreatech sqprod

Most helpful comment

Hi Mike,
I managed to solve my issue thanks to the link I've already provided in my comment (https://stackoverflow.com/questions/10485978/creating-sub-directory-via-sql-insert-using-filetable). I've already scoured the internet to find help and I've seen the link that you've provided. Unfortunately that link does not provide information that I need. The main problem I had is how to manipulate the FILETABLE (CRUD) via the T-SQL or (.NET) code. All the documentation and all online help that I've seen focuses on manipulating the files via the File Explorer.
In my case we have a different scenario. The files are exposed via network file share but only as read-only and our custom application does all the managing of the files.

In essence what I really needed was how to generate the path_locator field (hierarchyid) correctly.
You have to generate it in order to insert a new record but there seems to be no 'standard' way to do it. In the link I've provided in my comment the path_locator is build up as a string by using GUID parts.
This works ... but is it the 'right' way to do it or is it a hack? It would be nice if official documentation would provide this information.

Since we are at it some more questions that I have:

  • I figured out your could move a file to a different folder by T-SQL by simply setting new path_locator. Is it an 'OK' thing to do since it is a primary key of the table? I have no idea, again official documentation could be helpful here.
  • I am writing file content in ADO.NET code by creating SqlCommand for INSERT/UPDATE with a parameter for the file content and then assigning a Stream as a parameter value. AFAIK this is standard way you work with large varbinary fields from ADO.NET. Is this the 'recommended' way of inserting/updaring FILETABLE content or is there an alternative.

Hopefully this comment provides you some insight into what I needed.

@WilliamAntonRohm
Sorry I was being snarky in my initial comment. It was very frustrating to work on this since it is a bit of an obscure functionality of SQL Server.

All 7 comments

@dcarapic -- Dalibor, thank you for your feedback. Please see this article:

Please also consider these resources:

I am closing this issue now. You are welcome to @ mention me for any followup.
We hope to hear from you again.

cc @MikeRayMSFT for awareness

If you do not want to provide examples then please just state it.
You have linked an article which does not help at all (question is INSERT,
not Create, Alter or Drop Filetables).
Here, at least have this link somewhere so that people searching for help
can find some additional information:

https://stackoverflow.com/questions/10485978/creating-sub-directory-via-sql-insert-using-filetable

Thanks.

--
Dalibor Čarapić

On Tue, Jun 16, 2020 at 8:00 PM William Anton Rohm notifications@github.com
wrote:

@dcarapic https://github.com/dcarapic -- Dalibor, thank you for your
feedback. Please see this article:

Please also consider these resources:

I am closing this issue now. You are welcome to @ mention me for any
followup.
We hope to hear from you again.

cc @MikeRayMSFT https://github.com/MikeRayMSFT for awareness


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/MicrosoftDocs/sql-docs/issues/4981#issuecomment-644919878,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABM7OJCQFK5MSACG5X2MEZLRW6XKVANCNFSM4N7J425Q
.

@dcarapic -- Dalibor, thank you for your follow-up.

@MikeRayMSFT -- Mike, please look into this issue.

@dcarapic - thank you for the submission. I'm reviewing this internally and we'll respond.

@dcarapic - I really appreciate your patience. I'm working on staging a comprehensive example. I don't want you to have to continue to wait though. While I'm creating and staging that, make sure you check out this helpful example: https://www.sqlshack.com/sql-server-filetable-the-next-generation-of-sql-filestream/

Hi Mike,
I managed to solve my issue thanks to the link I've already provided in my comment (https://stackoverflow.com/questions/10485978/creating-sub-directory-via-sql-insert-using-filetable). I've already scoured the internet to find help and I've seen the link that you've provided. Unfortunately that link does not provide information that I need. The main problem I had is how to manipulate the FILETABLE (CRUD) via the T-SQL or (.NET) code. All the documentation and all online help that I've seen focuses on manipulating the files via the File Explorer.
In my case we have a different scenario. The files are exposed via network file share but only as read-only and our custom application does all the managing of the files.

In essence what I really needed was how to generate the path_locator field (hierarchyid) correctly.
You have to generate it in order to insert a new record but there seems to be no 'standard' way to do it. In the link I've provided in my comment the path_locator is build up as a string by using GUID parts.
This works ... but is it the 'right' way to do it or is it a hack? It would be nice if official documentation would provide this information.

Since we are at it some more questions that I have:

  • I figured out your could move a file to a different folder by T-SQL by simply setting new path_locator. Is it an 'OK' thing to do since it is a primary key of the table? I have no idea, again official documentation could be helpful here.
  • I am writing file content in ADO.NET code by creating SqlCommand for INSERT/UPDATE with a parameter for the file content and then assigning a Stream as a parameter value. AFAIK this is standard way you work with large varbinary fields from ADO.NET. Is this the 'recommended' way of inserting/updaring FILETABLE content or is there an alternative.

Hopefully this comment provides you some insight into what I needed.

@WilliamAntonRohm
Sorry I was being snarky in my initial comment. It was very frustrating to work on this since it is a bit of an obscure functionality of SQL Server.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ryepup picture ryepup  ·  3Comments

404
hfleitas picture hfleitas  ·  3Comments

binki picture binki  ·  4Comments

stevef51 picture stevef51  ·  4Comments

GeorgeTsiokos picture GeorgeTsiokos  ·  4Comments