Imagine I have a table like this in MSSQL
------------------------------------- file_name(varchar) | file_doc(image) | -------------------------------------- A | image_binary1 | -------------------------------------- B | image_binary2 | --------------------------------------
I want to save contents of this table to file system something like the following
- c:\somewhere\A.pdf - c:\somewhere\B.pdf
how to achive that in MSSQL?
Best Answer
The easiest way is to use
SSIS
: theExport Column
component does what you are looking for. Here is an example: Export images from a SQL Server Table to a Folder with SSISThis is not only the easiest way, it requires minimal permissions: only
SELECT
permission on the table and access to file folder.Also you can write a
powershell
script: Export SQL Server BLOB Data with PowerShellThe user should be able not only access a table (
SELECT
permission) but the rights to executepowershell
scripts.bcp
but only for 1 file at a time, so for multiple files you need to write a cursor and usexp_cmdshell
. Use ofxp_cmdshell
is not a best practice, it should be enabled on your server and you should have elevated permissions to be able to usexp_cmdshell
. In case ofbcp
you should useformat file
, otherwise your files will contain additional 8 bytes and have a wrong format.