SQL Server 2000: how to save an Image variable to a file in the file system from a stored procedure

后端 未结 1 332
猫巷女王i
猫巷女王i 2021-01-27 05:47

I have a stored procedure that receives a @Data image parameter.

And I want to save it to the file system from a stored procedure.

Any

1条回答
  •  温柔的废话
    2021-01-27 06:51

    You can use OLE automation (ADODB.Stream) to write binary data from a SQL Server 2000 stored procedure, as described here and here. The sample below uses a varbinary variable, but it should work similarly with your image parameter.

    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
    

    Note that access to the file system via this method (and all others in SQL2000 that I know) will occur under the security context of the SQL Server process, not the caller of the stored procedure.

    If you aren't comfortable loading the ADO components into your SQL Server process, you could look into implementing an extended stored procedure to write your files.

    0 讨论(0)
提交回复
热议问题