Sample code for 30+ languages & platforms
SQL Server

SFTP ReadLink - Get the Target of a Symbolic Link on the Server

See more SFTP Examples

Demonstrates how to retrieve the target of a symbolic link on the SFTP server.

Note: This example requires Chilkat v9.5.0.71 or greater.

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 assumes 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

    -- Pass a domain or IP address..
    EXEC sp_OAMethod @sftp, 'Connect', @success OUT, 'my-sftp-server.com', 22
    IF @success = 1
      BEGIN
        EXEC sp_OAMethod @sftp, 'AuthenticatePw', @success OUT, 'mySFtpLogin', 'mySFtpPassword'
      END
    IF @success = 1
      BEGIN
        EXEC sp_OAMethod @sftp, 'InitializeSftp', @success OUT
      END
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    -- In this example, the we already have a symbolic link named "sshd_config"
    -- in our SSH/SFTP user account's HOME directory.  Get the target of this link:
    DECLARE @path nvarchar(4000)
    EXEC sp_OAMethod @sftp, 'ReadLink', @path OUT, 'sshd_config'
    EXEC sp_OAGetProperty @sftp, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END


    PRINT 'symlink target path = ' + @path

    -- Output is:  
    -- symlink target path = /etc/ssh/sshd_confi

    EXEC @hr = sp_OADestroy @sftp


END
GO