HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlModerate

How to export an image column to files in SQL Server?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
imagecolumnsqlfilesexporthowserver

Problem

I will migrate from a database. There is one column of type image that I would like to export to binary files on the file system. One file for each record. How can I do this with SQL Server?

Solution

This is the solution I came up with:

-
Enable xp_cmdshell with

EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO


-
If needed create a directory with xp_cmdshell to get the needed permissions.

EXEC master..xp_cmdshell 'mkdir C:\exportdir'


-
Use BCP with queryout

EXEC master..xp_cmdshell 'BCP "SELECT column_of_type_image FROM **your_db WHERE id = 1 " queryout "C:\exportdir\yourfile.pdf" -T -N'


**your_db must be the fully qualified table name, i.e [Yourdb].[YourSchema].[YourTable]

Code Snippets

EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
EXEC master..xp_cmdshell 'mkdir C:\exportdir'
EXEC master..xp_cmdshell 'BCP "SELECT column_of_type_image FROM **your_db WHERE id = 1 " queryout "C:\exportdir\yourfile.pdf" -T -N'

Context

StackExchange Database Administrators Q#80817, answer score: 16

Revisions (0)

No revisions yet.