Sql Server 2008 R2: Best Way To Export Blob And Other Column Data Out To The File System
Solution 1:
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 sourceselect 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 importedselect*from Playground.dbo.BlobTest
exceptselect*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.
Solution 2:
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 DISCCREATETABLE [dbo].[tblTemp](
[ID] [int] IDENTITY(1,1) NOTNULL,
[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'''ALTERPROCEDURE WriteBinaryToDisc
(
@PathVARCHAR(255),
@FilenameVARCHAR(100),
@FileExtVARCHAR(4),
@TblNamevarchar(50),
@IDFieldVARCHAR(50),
@ImageFieldVARCHAR(50),
@WHEREVARCHAR(300)
)
ASset nocount onEXEC ('
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
')
---------------------------------------------------------------
Post a Comment for "Sql Server 2008 R2: Best Way To Export Blob And Other Column Data Out To The File System"