Sample code for 30+ languages & platforms
SQL Server

Co:Z SFTP Binary File Download (from z/OS IBM Mainframe)

See more SFTP Examples

Demonstrates how to download a binary file, such as a .zip, from a Co:Z SFTP server on a z/OS IBM Mainframe.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @sftp int
    EXEC @hr = sp_OACreate 'Chilkat.SFtp', @sftp OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Connect to the SSH server.  
    DECLARE @hostname nvarchar(4000)
    SELECT @hostname = 'sftp.example.com'
    DECLARE @port int
    SELECT @port = 22
    EXEC sp_OAMethod @sftp, 'Connect', @success OUT, @hostname, @port
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    EXEC sp_OAMethod @sftp, 'AuthenticatePw', @success OUT, 'myLogin', 'myPassword'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    EXEC sp_OAMethod @sftp, 'InitializeSftp', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    -- To download a binary file from the Co:Z SFTP server, 
    -- we must switch to binary mode in the following unconventional way.
    -- We pretend to fetch a directory listing for "/+mode=binary"
    -- This has the effect of putting the server in binary mode for transfers.
    DECLARE @handle nvarchar(4000)
    EXEC sp_OAMethod @sftp, 'OpenDir', @handle OUT, '/+mode=binary'
    EXEC sp_OAGetProperty @sftp, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    -- Download the "directory listing" (but it's not actually a directory listing, and we'll just discard it.)

    DECLARE @dirListing int
    EXEC @hr = sp_OACreate 'Chilkat.SFtpDir', @dirListing OUT

    EXEC sp_OAMethod @sftp, 'ReadDirListing', @success OUT, @handle, @dirListing
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        EXEC @hr = sp_OADestroy @dirListing
        RETURN
      END

    -- Close the directory handle:
    EXEC sp_OAMethod @sftp, 'CloseHandle', @success OUT, @handle
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        EXEC @hr = sp_OADestroy @dirListing
        RETURN
      END

    -- Download the binary file:
    DECLARE @localFilePath nvarchar(4000)
    SELECT @localFilePath = 'c:/temp/test.zip'
    DECLARE @remoteFilePath nvarchar(4000)
    SELECT @remoteFilePath = 'test.zip'
    EXEC sp_OAMethod @sftp, 'DownloadFileByName', @success OUT, @remoteFilePath, @localFilePath
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        EXEC @hr = sp_OADestroy @dirListing
        RETURN
      END


    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @sftp
    EXEC @hr = sp_OADestroy @dirListing


END
GO