2011-12-05 37 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

相關問題