[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 3:51 PM
# 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 12:49 PM
# 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 1:29 PM
# 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 11: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 11: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 08, 2008 3:09 AM
# 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 28, 2008 3:01 AM
# 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

What do you have to say?

(will show your gravatar)
Please add 5 and 4 and type the answer here: