SQL Server 2008 R2: Best way to export BLOB and other column data out to the file system

StackOverflow https://stackoverflow.com/questions/17992254

  •  04-06-2022
  •  | 
  •  

Question

I have read some arts about BCP or CLR code that will export BLOBs to individual files on the hard drive, but I need to BCP or CLR out the entire table data together (meaning the other columns which are character or integer or datetime data need to come out as a whole) I need to make sure I can BCP/CLR the data back into the table and have the same linking between BLOBs and other column data.

Any ideas?

Was it helpful?

Solution

I'm not sure if I understand what you're asking, so I'll try to cover two cases.

First, if you'd like to export all your data (including varbinary blobs) into one file, you can do it. Here's a test script to use with your table. You have to turn on SQLCMD mode in your SSMS. Then issue this script:

-- create target structure same as source
select top 0 *
into YourTbl2
from YourTbl

-- first line makes BCP dump to file, second line imports it to target structure    
!!bcp YourDb.dbo.YourTbl out "D:\Temp\BlobTest.bak" -T -c 
!!bcp YourDb.dbo.YourTbl2 in "D:\Temp\BlobTest.bak" -T -c 

-- test if everything is imported
select * from Playground.dbo.BlobTest
except
select * from Playground.dbo.BlobTest2

If you want to just export an individual file to disk where your SQL Server resides, you can use this:

!!bcp "SELECT TOP 1 YourBlob FROM YourDb.dbo.YourTbl" queryout "D:\Temp\YourFile.xyz" -T -c

If it's applicable, you can share the folder where you're exporting your blob and access it from your client PC.

OTHER TIPS

You can do like the Following : You can have Image Data Type to Hold Any Files with in. And, Please Read the Following Thoroughly to Understand. I've implemented this in our Project. Simple and Fully Dynamic. You just have to call :

Insert [tblTemp] (imageType,ImageFile) Select '.PDF',BulkColumn from Openrowset( Bulk     'C:\mak\A.PDF', Single_Blob) as tb

to Insert into the Table and You can Use :

 WriteBinaryToDisc 'C:\NEWF\','MAK','.PDF','DOC_TABLE','DOC_ID','DOC_IMAGE','WHERE     DOC_ID=''25'''

to write back to the File System with Your specified Location and Extension.

I've used an tblTemp to hold all the Files.

--FIRST CHANGE THE CONFIGURATION TO ACTIVATE THIS FEATURE
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO

--HOW TO WRITE FILES TO DIRECTLY SQL SERVER FROM DISC
CREATE TABLE [dbo].[tblTemp](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [ImageType] [varchar] (10) NULL,
    [ImageFile] [image] NULL
) ON [PRIMARY]

Insert [tblTemp] (imageType,ImageFile) Select '.PDF',BulkColumn from Openrowset( Bulk     'C:\mak\A.PDF', Single_Blob) as tb
-----------------------------------------------------

--HOW TO WRITE FILE TO DISC FROM SQL SERVER
--WriteBinaryToDisc 'C:\NEWF\','MAK','.PDF','DOC_TABLE','DOC_ID','DOC_IMAGE','WHERE     DOC_ID=''25'''
ALTER PROCEDURE WriteBinaryToDisc 
(
    @Path VARCHAR(255),
    @Filename VARCHAR(100),
    @FileExt VARCHAR(4),
    @TblName varchar(50),
    @IDField VARCHAR(50),
    @ImageField VARCHAR(50),
    @WHERE VARCHAR(300)
)
AS
set nocount on
EXEC ('
DECLARE @SOURCEPATH VARBINARY(MAX),
@DESTPATH VARCHAR(MAX),
@ObjectToken INT,
@image_ID BIGINT

DECLARE IMGPATH CURSOR FAST_FORWARD FOR SELECT '+@ImageField+','+@IDField+' from     '+@TblName+' '+@WHERE+' 
OPEN IMGPATH

FETCH NEXT FROM IMGPATH INTO @SOURCEPATH, @image_ID

WHILE @@FETCH_STATUS = 0
BEGIN
SET @DESTPATH = '''+@Path+'\'+@Filename+'''+ CAST(@image_ID AS     varchar)+'''+@FileExt+'''

EXEC sp_OACreate ''ADODB.Stream'', @ObjectToken OUTPUT
EXEC sp_OASetProperty @ObjectToken, ''Type'', 1
EXEC sp_OAMethod @ObjectToken, ''Open''
EXEC sp_OAMethod @ObjectToken, ''Write'', NULL, @SOURCEPATH
EXEC sp_OAMethod @ObjectToken, ''SaveToFile'', NULL, @DESTPATH, 2
EXEC sp_OAMethod @ObjectToken, ''Close''
EXEC sp_OADestroy @ObjectToken

FETCH NEXT FROM IMGPATH INTO @SOURCEPATH, @image_ID
END

CLOSE IMGPATH
DEALLOCATE IMGPATH
')
---------------------------------------------------------------
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top