多亏了这篇文章,我已经弄清楚了...
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 ""
关于该格式文件的最后说明.. 在最后一行之后必须有一个新行。否则你会得到一个错误。
享受!