Search code examples
sql-serverbcp

Using bcp with filename taken from another column


Using bcp, I want to save all .docx files based on contents stored in a column (content) of a table T in SQL Server.

|--|-------|
|id|content|
|--|-------|
|12|0xraert|
|13|0xrteot|
|14|0xrtezt|
|15|0xrteta|
|..| ...   |

A working request, for one document (that has id=13), is the following:

bcp "SELECT content FROM T WHERE id = 13" \
queryout "output\path\13.docx" \
-S xxx  \
-d xxx  \
-T \
-C \
RAW  \
< path\to\parameters.txt

How can I make a query such that all contents go saved to {id}.docx? I can make a shell loop with random indices but I'd want filenames to match exactly id columns...

Thanks a lot!


Solution

  • BCP will only create a single file per execution of BCP. To do what you are asking would require something similar to what you stated. A separate process to build and execute a BCP statement for each file you want to create. This cannot be done in a single query and execution of BCP.