我有一个表,里面有一个IDENTITY
字段和一个VARBINARY(MAX)
字段.有什么办法可以将每个VARBINARY(MAX)
字段的内容转储到文件系统中(例如c:\temp\images
)?
PhotoId INTEGER NOT NULL IDENTITY Image VARBINARY(MAX) NOT NULL DateCreated SMALLDATETIME
c:\temp\images\1.png c:\temp\images\2.png c:\temp\images\3.png ... etc...
解决这个问题的最佳方法是什么?
我已经想通了这篇帖子 ......
SET NOCOUNT ON DECLARE @IdThumbnail INTEGER, @MimeType VARCHAR(100), @FileName VARCHAR(200), @Sqlstmt varchar(4000) DECLARE Cursor_Image CURSOR FOR SELECT a.IdThumbnail FROM tblThumbnail a ORDER BY a.IdThumbnail OPEN Cursor_Image FETCH NEXT FROM Cursor_Image INTO @IdThumbnail WHILE @@FETCH_STATUS = 0 BEGIN -- Generate the file name based upon the ID and the MIMETYPE. SELECT @FileName = LTRIM(STR(@IdThumbnail)) + '.png' -- Framing DynamicSQL for XP_CMDshell SET @Sqlstmt='BCP "SELECT OriginalImage FROM Appian.dbo.tblThumbnail WHERE IdThumbnail = ' + LTRIM(STR(@IdThumbnail)) + '" QUERYOUT c:\Temp\Images\' + LTRIM(@FileName) + ' -T -fC:\Temp\ImageFormatFile.txt' print @FileName print @sqlstmt EXEC xp_cmdshell @sqlstmt FETCH NEXT FROM Cursor_Image INTO @IdThumbnail END CLOSE Cursor_Image DEALLOCATE Cursor_Image
请注意 - >你需要有一个格式文件,用于BCP命令.这是文件的内容,我把它放在c:\ Temp中(如上面的BCP命令行中所述).
10.0 1 1 SQLIMAGE 0 0 "" 1 OriginalImage ""
关于那个格式文件的最后注释..在最后一行之后有一个新线.否则你会收到错误.
请享用!