您可以使用 OLE 自动化 (ADODB.Stream) 从 SQL Server 2000 存储过程写入二进制数据,如下所述here http://www.motobit.com/tips/detpg_read-write-binary-files/ and here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=101754。下面的示例使用 varbinary 变量,但它应该与您的图像参数类似地工作。
DECLARE @Path VarChar(255)
DECLARE @Data VarBinary(1000)
SET @Data = 0x12345678
SET @Path = 'c:\test.dat'
DECLARE @object Integer
DECLARE @hr Integer
-- create the ADO stream object
EXEC @hr = sp_oacreate 'ADODB.Stream', @object OUTPUT, 1
IF (@hr <> 0)
RAISERROR('ADO stream creation failed', 11, 0)
-- configure it for binary access
EXEC @hr = sp_oasetproperty @object, 'Type', 1
IF (@hr <> 0)
RAISERROR('Failed to set stream type', 11, 0)
-- open the stream
EXEC @hr = sp_oamethod @object, 'Open'
IF (@hr <> 0)
RAISERROR('Failed to open the stream object', 11, 0)
-- write the buffer to the stream
EXEC @hr = sp_oamethod @object, 'Write', NULL, @Data
IF (@hr <> 0)
RAISERROR('Failed to write a buffer to the stream', 11, 0)
-- save the stream to a file with overwrite
EXEC @hr = sp_oamethod @object, 'SaveToFile', NULL, @Path, 2
IF (@hr <> 0)
RAISERROR('Failed to save the stream to a file', 11, 0)
-- cleanup
EXEC sp_oadestroy @object
请注意,通过此方法(以及我所知道的 SQL2000 中的所有其他方法)对文件系统的访问将发生在 SQL Server 进程的安全上下文中,而不是存储过程的调用者。
如果您不习惯将 ADO 组件加载到 SQL Server 进程中,您可以考虑实现一个扩展存储过程 http://msdn.microsoft.com/en-us/library/aa215790%28v=sql.80%29.aspx写入您的文件。