cnblogs/dcrenl/sql server 使用sql语句导出二进制文件到本地磁盘.html
2024-09-24 12:43:01 +08:00

12 lines
1.5 KiB
HTML

<p>sp_configure 'show advanced options', 1;<br />GO<br />RECONFIGURE;<br />GO<br />sp_configure 'Ole Automation Procedures', 1;<br />GO<br />RECONFIGURE;<br />GO</p>
<p>DECLARE @SOURCEPATH VARBINARY(MAX),<br />@DESTPATH VARCHAR(MAX),<br />@ObjectToken INT,<br />@image_ID VARCHAR(MAX),<br />@ExamID varchar(50),<br />@DataBaseName varchar(50)</p>
<p>DECLARE IMGPATH CURSOR FAST_FORWARD FOR</p>
<p>--********************************************************************************************************************************--<br />--替换为你的查询语句</p>
<p>SELECT 文件内容,文件名称 from 表</p>
<p>--********************************************************************************************************************************--</p>
<p>OPEN IMGPATH</p>
<p>FETCH NEXT FROM IMGPATH INTO @SOURCEPATH, @image_ID</p>
<p>WHILE @@FETCH_STATUS = 0<br />BEGIN<br />--替换为你的路径<br />SET @DESTPATH = 'D:\' + @image_ID + '.jpg'<br />print(@image_ID)<br />print(@DESTPATH)</p>
<p>EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT<br />EXEC sp_OASetProperty @ObjectToken, 'Type', 1<br />EXEC sp_OAMethod @ObjectToken, 'Open'<br />EXEC sp_OAMethod @ObjectToken, 'Write', NULL, @SOURCEPATH<br />EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, @DESTPATH, 2<br />EXEC sp_OAMethod @ObjectToken, 'Close'<br />EXEC sp_OADestroy @ObjectToken</p>
<p>FETCH NEXT FROM IMGPATH INTO @SOURCEPATH, @image_ID<br />END</p>
<p>CLOSE IMGPATH<br />DEALLOCATE IMGPATH<br />print('导出完成')</p>