Sample code for 30+ languages & platforms
SQL Server

Download Text from an SFTP Server (StringBuilder)

See more SFTP Examples

Demonstrates the Chilkat SFtp.DownloadSb method, which downloads a remote text file, decodes it with the given charset, and appends the text to a StringBuilder. The arguments are the remote file path, the charset, and the StringBuilder.

Note: This example uses relative local paths, which are resolved against the application's current working directory. Absolute local paths may also be used. Supply the paths appropriate to your own environment.

Background: When the remote file is text and you want its content as characters rather than raw bytes, this decodes it in one step so you can parse or display it directly. The charset must match how the file is actually encoded, or the text will be garbled. Like DownloadBd it appends, so clear the StringBuilder first if you want only this file's text.

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
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the SFtp.DownloadSb method, which downloads a remote text file, decodes it, and
    --  appends the text to a StringBuilder.  The 1st argument is the remote file path, the 2nd is the
    --  charset, and the 3rd is the StringBuilder.

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

    --  Connect, authenticate, and initialize the SFTP subsystem.
    DECLARE @port int
    SELECT @port = 22
    EXEC sp_OAMethod @sftp, 'Connect', @success OUT, 'sftp.example.com', @port
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    --  Normally you would not hard-code the password in source.  You should instead obtain it
    --  from an interactive prompt, environment variable, or a secrets vault.
    DECLARE @password nvarchar(4000)
    SELECT @password = 'mySshPassword'

    EXEC sp_OAMethod @sftp, 'AuthenticatePw', @success OUT, 'mySshLogin', @password
    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

    --  Download the remote text file, decoding it as UTF-8.  Existing text in the StringBuilder is
    --  preserved, so clear it first if you want replacement rather than append behavior.
    DECLARE @sb int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT

    EXEC sp_OAMethod @sftp, 'DownloadSb', @success OUT, 'subdir/inventory.csv', 'utf-8', @sb
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        EXEC @hr = sp_OADestroy @sb
        RETURN
      END


    EXEC sp_OAGetProperty @sb, 'Length', @iTmp0 OUT
    PRINT 'Characters downloaded: ' + @iTmp0
    DECLARE @fileText nvarchar(4000)
    EXEC sp_OAMethod @sb, 'GetAsString', @fileText OUT

    PRINT @fileText

    EXEC sp_OAMethod @sftp, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @sftp
    EXEC @hr = sp_OADestroy @sb


END
GO