[SQL] Stored Procedure To FTP Files From SQL Server

This is another useful Sql Server Stored Procedure I found on the net written by Nigel Rivett.

The procedure uses the xp_cmdshell extended stored procedure to shell out an FTP command. You can use this procedure to ftp a file from one place to another. Of course, you will need to make sure that your command runs in the proper security context.

I made some very slight modifications in my own version of this procedure. I changed some of the parameters to be of type nvarchar instead of varchar for my international friends. I also changed the name to suit my own naming conventions.

It takes in the following parameters.

Parameter Data Type Description Example
@FTPServer varchar(128) The host name. ftp.example.com
@FTPUser nvarchar(128) The username for the FTP site. Haacked
@FTPPWD nvarchar(128) The password for the FTP site. Ha!_AsIfIWouldTellYou!
@FTPPath nvarchar(128) The subfolder within the FTP site to place the file. Make sure to use forward slashes and leave a trailing slash. /
@FTPFileName nvarchar(128) The filename to write within FTP. Typically the same as the source file name. ImportantFile.zip
@SourcePath nvarchar(128) The path to the directory that contains the source file. Make sure to have a trailing slash. c:\projects\
@SourceFile nvarchar(128) The source file to ftp. ImportantFile.zip
@workdir nvarchar(128) The working directory. This is where the stored proc will temporarily write a command file containing the FTP commands it will execute. c:\temp\

Here is an example of the usage.

exec FtpPutFile     
    @FTPServer = 'ftp.example.com' ,
    @FTPUser = n'username' ,
    @FTPPWD = n'password' ,
    @FTPPath = n'/dir1/' ,
    @FTPFileName = n'test2.txt' ,
    @SourcePath = n'c:\vss\mywebsite\' ,
    @SourceFile = n'MyFileName.html' ,
    @workdir = n'c:\temp\'

I will soon combine this and my random time of day generator sql into a very useful stored procedure for you.

Technorati Tags: ,

What others have said

Requesting Gravatar... Barry Dorrans Apr 21, 2006 7:51 AM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
And don't forget, by default, xp_cmdshell is disabled in SQL 2005.
Requesting Gravatar... shweta Aug 03, 2006 4:49 AM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
do you know how to do a secured ftp from SQL
Requesting Gravatar... Raz Jan 04, 2007 5:29 AM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
Can you use this to transfer data from different servers, for example I want to ftp *.dat files to my sql server database from another secure server. Can I use this code to locate these files.

Thanks
Requesting Gravatar... colin Mar 30, 2007 3:07 AM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
Hello there,

I tried to execute this sp from a DTS package task and keep getting the error "Invalid object name '#a'".

Any clue as to what I can do t fix or why it doesn't see #a- its part of the sp as a temp table, right?

Thanks in advance
Requesting Gravatar... Haacked Mar 30, 2007 3:39 AM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
Weird. If you look at the query, there's the following snippet.


create table #a (id int identity(1,1), s varchar(1000))
insert #a exec master..xp_cmdshell @cmd


The first line creates the temp table, the second line inserts into it. Not sure why that wouldn't work as part of a DTS package.
Requesting Gravatar... Art Jan 07, 2008 7:09 PM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
do you know how to do ,or if exists, a stored procedure to SQL Server "table" From FTP Files? Thanks
Requesting Gravatar... Oscar Ågren Mar 27, 2008 7:01 PM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
You said that you made some modifications to the code. But you didn't post the new source code?

Cheers
Requesting Gravatar... Frederick Sep 04, 2008 11:34 PM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
Someone or the author can update this site?
Seems like not moderated....
Requesting Gravatar... Thomas Mucha Nov 24, 2008 1:02 AM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
This was very helpful. Previously we had been generating an ftp script file using vbs (not my strong point) and I couldn't easily pass date parameters to it. Now it's solved!
Requesting Gravatar... hhgregg Feb 01, 2011 5:59 AM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
Thanks for sharing this, Phil! We use to accomplish this using a VB Script where I couldn’t pass date parameters. Is it possible to do a secured FTP from the stored procedure? Did you post the new source code that combines the Stored Procedure FTP with the Time of Day generator?
Requesting Gravatar... recruiting software Oct 05, 2011 11:30 PM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
Good call, Barry. I kept forgetting about xp_cmdshell being disabled in 2005 and it almost drove me insane.
I look forward to seeing what you're using this code for. I like it, but for the life of me, I can't figure out what it's good for! Ha.

-Jameson
Requesting Gravatar... venkat Jan 14, 2012 2:00 AM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
Can I ftp files(.dat files) from a sql server to a unix server.
If so any suggestions how it can be implemented.
Requesting Gravatar... Marcel Apr 21, 2012 1:09 PM
# re: [SQL] Stored Procedure To FTP Files From SQL Server
I keep getting error on authentication, while when using the same credentials in manually FTP there is no problem. Could there be a way to put the credentials in one single command?

What do you have to say?

(will show your gravatar)
Please add 1 and 7 and type the answer here: