2011-12-05 30 views

回答

2

您可以使用OLE自动化(ADODB.Stream)从SQL Server 2000存储过程写入二进制数据,如herehere所述。下面的示例使用varbinary变量,但它应该与图像参数类似。即通过这种方法(和SQL2000,我知道所有其他人)访问文件系统

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 

注意SQL Server进程,而不是存储过程的调用者的安全环境下会发生。

如果您不习惯将ADO组件加载到SQL Server进程中,则可以考虑实施extended stored procedure来编写文件。

+0

伟大的提示,我会试一试,然后我会告诉你 – opensas

+0

非常感谢,布伦特,它工作正常 – opensas

相关问题