On several occasions I have had to use SSIS to export data from a SQL database, and then transfer that data to a third party via sFTP. SQL Server SSIS has an FTP task built in but it does not natively support sFTP. There are several commercial products (Eldos Software, /n Software, CozyRoc) out on the market that can be used to add that functionality into SSIS but I am going to talk about using a free utility called WinSCP to accomplish this task.
The first step is to download and install WinSCP, you will need to do this on the SQL server that will be running the SSIS package. Once you have installed WinSCP you should create a connection to the distant sFTP server in WinSCP so you can test and confirm that you can connect to it. Create and name a saved session so that you can call that session from your script later. I will name this session “MySecureFTP”.
Image may be NSFW.
Clik here to view.
Once you have done that you will need to create a WinSCP script to transfer your files. For examples of scripts see the WinSCP documentation. Here is a very basic script that I created to copy a csv file.
option batch on
option confirm off
open MySecureFTP
put c:\outbound\*.csv /inbounddirectory/
exit
After testing the script by running it from a command prompt and confirming that it works, you are ready to step into SSIS and call it from your workflow.
In Microsoft Visual Studio in your SSIS Package, go to the Control Flow tab and from your toolbox add an “Execute Process Task” to the workflow. It should look like this after you’ve added it.
Image may be NSFW.
Clik here to view.
Double click the Execute Process Task to open the Task Editor. You should now have a window like this.
Image may be NSFW.
Clik here to view.
Go to the Process section on the left and then you will need to fill out some information.
Image may be NSFW.
Clik here to view.
In the executable line you will need to fill out the correct path to the WinSCP executable. Mine was in the (x86) directory but it may be different for your computer.
Then in the Arguments line you will need to put the /script= switch and then the path to that script. I also included the /log= switch to log the execution to a file. That way I can go back and review the FTP connection and make sure everything was a success.
You may need to add an entry for the WorkingDirectory if you do not use absolute paths in your script. I didn’t require it.
Now you can build your SSIS package and deploy it. That’s all that you need to do.
One last comment that I’ve found is that if you’ve created a stored session in WinSCP it stores the value in the registry under the HKey\Current_User folder and depending on what credentials you use to run the SQL Job you may not have access to that registry key. One option to fix this is to switch WinSCP to use an INI so all the stored sessions are stored in the INI. That way the script can still access the stored session. Another way to fix this is to set the SQL Job to run as a different user but that involves setting up a Proxy in SQL Server Agent which I will cover in another post.
Filed under: SQL Server Tagged: sFTP, SQL Server, SSIS Image may be NSFW.
Clik here to view.
