Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used below query for downloading a file from FTP.
SP is executes successfully but files are not getting downloaded.

Can any body help me to download file from FTP through SQL Stored Procedure?
SQL
Create procedure s_ftp_GetFile
@FTPServer	varchar(128) ,
@FTPUser	varchar(128) ,
@FTPPWD		varchar(128) ,
@FTPPath	varchar(128) ,
@FTPFileName	varchar(128) ,
@SourcePath	varchar(128) ,
@SourceFile	varchar(128) ,
@workdir	varchar(128)
as
/*
exec s_ftp_GetFile 	
		@FTPServer = 'www.myftpsite.com' ,
		@FTPUser = 'myuser' ,
		@FTPPWD = 'mypwd' ,
		@FTPPath = '' ,
		@FTPFileName = 'myfile.html' ,
		@SourcePath = 'c:\vss\mywebsite\' ,
		@SourceFile = 'myfile.html' ,
		@workdir = 'c:\temp\'
*/
declare	@cmd varchar(1000)
declare @workfilename varchar(128)
	
	select @workfilename = 'ftpcmd.txt'
	
	-- deal with special characters for echo commands
	select @FTPServer = replace(replace(replace(@FTPServer, '|', '^|'),'<','^<'),'>','^>')
	print @FTPServer
	select @FTPUser = replace(replace(replace(@FTPUser, '|', '^|'),'<','^<'),'>','^>')
	print @FTPUser
	select @FTPPWD = replace(replace(replace(@FTPPWD, '|', '^|'),'<','^<'),'>','^>')
	print @FTPPWD 
	select @FTPPath = replace(replace(replace(@FTPPath, '|', '^|'),'<','^<'),'>','^>')
	print @FTPPWD 
	
	select	@cmd = 'echo '					+ 'open ' + @FTPServer
			+ ' > ' + @workdir + @workfilename
	exec master..xp_cmdshell @cmd
	select	@cmd = 'echo '					+ @FTPUser
			+ '>> ' + @workdir + @workfilename
	exec master..xp_cmdshell @cmd
	select	@cmd = 'echo '					+ @FTPPWD
			+ '>> ' + @workdir + @workfilename
	exec master..xp_cmdshell @cmd
	select	@cmd = 'echo '					+ 'get ' + @FTPPath + @FTPFileName + ' ' + @SourcePath + @SourceFile
			+ ' >> ' + @workdir + @workfilename
	exec master..xp_cmdshell @cmd
	select	@cmd = 'echo '					+ 'quit'
			+ ' >> ' + @workdir + @workfilename
	exec master..xp_cmdshell @cmd
	
	select @cmd = 'ftp -s:' + @workdir + @workfilename
	
	create table #a (id int identity(1,1), s varchar(1000))
	insert #a
	exec master..xp_cmdshell @cmd
	
	select id, ouputtmp = s from #a

GO
Posted
Updated 23-May-10 23:18pm
v2

1 solution

Have a look at this one:
Send Files Via FTP Using a Stored Procedure[^]

Looks like you are missing something like this at the end:
SQL
set @cmd = 'ftp -s:' + @script_file_path + @script_file_name
exec master..xp_cmdshell @cmd
 
Share this answer
 
Comments
Sagar.H.Mistry 24-May-10 5:41am    
thanks for reply... i have add thoes things also but still its not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900