In regular life of coding we are downloading data/files from server using c# code or any other code it may take much time to code as well as more time to download. This same thing you can be done using SQL server with 'Mput' command of FTP which is much lesser to code and will take a bit of time to download. Inverse process is also possible as you can use 'MPut' method.

You can use following script to download file(s) from FTP Server. Just pass your actual attributes and you have done, your files are downloaded.

MGet Command
  1. -- FTP_MGET.sql (Author Saddamhusen Uadanwala)
  2. -- Transfer all files from an FTP server Direcoty using MGET command.
  3. DECLARE @FTPServer varchar(128)
  4. DECLARE @FTPUserName varchar(128)
  5. DECLARE @FTPPassword varchar(128)
  6. DECLARE @SourcePath varchar(128)
  7. DECLARE @SourceFiles varchar(128)
  8. DECLARE @DestinationPath varchar(128)
  9. DECLARE @FTPMode varchar(10)
  10. -- Attributes
  11. SET @FTPServer = 'ftpserver'
  12. SET @FTPUserName = 'username'
  13. SET @FTPPassword = 'password'
  14. SET @SourcePath = '' -- Folder path/Blank for root directory.
  15. SET @SourceFiles = '*.csv'
  16. SET @DestinationPath = 'D:\Husen\Download' -- Destination path.
  17. SET @FTPMode = 'binary' -- binary, ascii or blank for default mode.
  18. DECLARE @Command varchar(1000)
  19. DECLARE @workfile varchar(128)
  20. DECLARE @nowstr varchar(25)
  21. -- %TEMP% environment variable.
  22. DECLARE @tempdir varchar(128)
  23. CREATE TABLE #tempvartable(info VARCHAR(1000))
  24. INSERT #tempvartable EXEC master..xp_cmdshell 'echo %temp%'
  25. SET @tempdir = (SELECT top 1 info FROM #tempvartable)
  26. IF RIGHT(@tempdir, 1) <> '\' SET @tempdir = @tempdir + '\'
  27. DROP TABLE #tempvartable
  28. -- Generate @workfile
  29. SET @nowstr = replace(replace(convert(varchar(30), GETDATE(), 121), ' ', '_'), ':', '-')
  30. SET @workfile = 'FTP_SPID' + convert(varchar(128), @@spid) + '_' + @nowstr + '.txt'
  31. -- special chars for echo commands.
  32. select @FTPServer = replace(replace(replace(@FTPServer, '|', '^|'),'<','^<'),'>','^>')
  33. select @FTPUserName = replace(replace(replace(@FTPUserName, '|', '^|'),'<','^<'),'>','^>')
  34. select @FTPPassword = replace(replace(replace(@FTPPassword, '|', '^|'),'<','^<'),'>','^>')
  35. select @SourcePath = replace(replace(replace(@SourcePath, '|', '^|'),'<','^<'),'>','^>')
  36. IF RIGHT(@DestinationPath, 1) = '\' SET @DestinationPath = LEFT(@DestinationPath, LEN(@DestinationPath)-1)
  37. -- Build the FTP script file.
  38. select @Command = 'echo ' + 'open ' + @FTPServer + ' > ' + @tempdir + @workfile
  39. EXEC master..xp_cmdshell @Command
  40. select @Command = 'echo ' + @FTPUserName + '>> ' + @tempdir + @workfile
  41. EXEC master..xp_cmdshell @Command
  42. select @Command = 'echo ' + @FTPPassword + '>> ' + @tempdir + @workfile
  43. EXEC master..xp_cmdshell @Command
  44. select @Command = 'echo ' + 'prompt ' + ' >> ' + @tempdir + @workfile
  45. EXEC master..xp_cmdshell @Command
  46. IF LEN(@FTPMode) > 0
  47. BEGIN
  48. select @Command = 'echo ' + @FTPMode + ' >> ' + @tempdir + @workfile
  49. EXEC master..xp_cmdshell @Command
  50. END
  51. select @Command = 'echo ' + 'lcd ' + @DestinationPath + ' >> ' + @tempdir + @workfile
  52. EXEC master..xp_cmdshell @Command
  53. IF LEN(@SourcePath) > 0
  54. BEGIN
  55. select @Command = 'echo ' + 'cd ' + @SourcePath + ' >> ' + @tempdir + @workfile
  56. EXEC master..xp_cmdshell @Command
  57. END
  58. select @Command = 'echo ' + 'mget ' + @SourcePath + @SourceFiles + ' >> ' + @tempdir + @workfile
  59. EXEC master..xp_cmdshell @Command
  60. select @Command = 'echo ' + 'quit' + ' >> ' + @tempdir + @workfile
  61. EXEC master..xp_cmdshell @Command
  62. -- Execute the FTP command via above generated script file.
  63. select @Command = 'ftp -s:' + @tempdir + @workfile
  64. create table #a (id int identity(1,1), s varchar(1000))
  65. print @Command
  66. insert #a
  67. EXEC master..xp_cmdshell @Command
  68. select id, ouputtmp = s from #a
  69. -- drop table.
  70. drop table #a
  71. select @Command = 'del ' + @tempdir + @workfile
  72. print @Command
  73. EXEC master..xp_cmdshell @Command

You can use following script to Upload file(s) from local directory to FTP Server. Replace default attributes to actual attributes.

MPut Command
  1. -- FTP_MPUT.sql (Author Saddamhusen Uadanwala)
  2. -- Transfer all files from an FTP server Direcoty using MPut command.
  3. DECLARE @FTPServer varchar(128)
  4. DECLARE @FTPUserName varchar(128)
  5. DECLARE @FTPPassword varchar(128)
  6. DECLARE @SourcePath varchar(128)
  7. DECLARE @SourceFiles varchar(128)
  8. DECLARE @DestinationPath varchar(128)
  9. DECLARE @FTPMode varchar(10)
  10. -- Attributes
  11. SET @FTPServer = 'ftpserver'
  12. SET @FTPUserName = 'username'
  13. SET @FTPPassword = 'password'
  14. SET @SourcePath = 'D:\Husen\Upload' -- Destination path.
  15. SET @SourceFiles = '*.csv'
  16. SET @DestinationPath = '' -- Folder path/Blank for root directory.
  17. SET @FTPMode = 'binary' -- binary, ascii or blank for default mode.
  18. DECLARE @Command varchar(1000)
  19. DECLARE @workfile varchar(128)
  20. DECLARE @nowstr varchar(25)
  21. -- %TEMP% environment variable.
  22. DECLARE @tempdir varchar(128)
  23. CREATE TABLE #tempvartable(info VARCHAR(1000))
  24. INSERT #tempvartable EXEC master..xp_cmdshell 'echo %temp%'
  25. SET @tempdir = (SELECT top 1 info FROM #tempvartable)
  26. IF RIGHT(@tempdir, 1) <> '\' SET @tempdir = @tempdir + '\'
  27. DROP TABLE #tempvartable
  28. -- Generate @workfile
  29. SET @nowstr = replace(replace(convert(varchar(30), GETDATE(), 121), ' ', '_'), ':', '-')
  30. SET @workfile = 'FTP_SPID' + convert(varchar(128), @@spid) + '_' + @nowstr + '.txt'
  31. -- Deal with special chars for echo commands.
  32. select @FTPServer = replace(replace(replace(@FTPServer, '|', '^|'),'<','^<'),'>','^>')
  33. select @FTPUserName = replace(replace(replace(@FTPUserName, '|', '^|'),'<','^<'),'>','^>')
  34. select @FTPPassword = replace(replace(replace(@FTPPassword, '|', '^|'),'<','^<'),'>','^>')
  35. select @DestinationPath = replace(replace(replace(@DestinationPath, '|', '^|'),'<','^<'),'>','^>')
  36. IF RIGHT(@SourcePath, 1) <> '\' SET @SourcePath = @SourcePath + '\'
  37. -- Build the FTP script file.
  38. select @Command = 'echo ' + 'open ' + @FTPServer + ' > ' + @tempdir + @workfile
  39. EXEC master..xp_cmdshell @Command
  40. select @Command = 'echo ' + @FTPUserName + '>> ' + @tempdir + @workfile
  41. EXEC master..xp_cmdshell @Command
  42. select @Command = 'echo ' + @FTPPassword + '>> ' + @tempdir + @workfile
  43. EXEC master..xp_cmdshell @Command
  44. select @Command = 'echo ' + 'prompt ' + ' >> ' + @tempdir + @workfile
  45. EXEC master..xp_cmdshell @Command
  46. IF LEN(@FTPMode) > 0
  47. BEGIN
  48. select @Command = 'echo ' + @FTPMode + ' >> ' + @tempdir + @workfile
  49. EXEC master..xp_cmdshell @Command
  50. END
  51. IF LEN(@DestinationPath) > 0
  52. BEGIN
  53. select @Command = 'echo ' + 'cd ' + @DestinationPath + ' >> ' + @tempdir + @workfile
  54. EXEC master..xp_cmdshell @Command
  55. END
  56. select @Command = 'echo ' + 'mput ' + @SourcePath + @SourceFiles + ' >> ' + @tempdir + @workfile
  57. EXEC master..xp_cmdshell @Command
  58. select @Command = 'echo ' + 'quit' + ' >> ' + @tempdir + @workfile
  59. EXEC master..xp_cmdshell @Command
  60. -- Execute the FTP command via above generated script file.
  61. select @Command = 'ftp -s:' + @tempdir + @workfile
  62. create table #a (id int identity(1,1), s varchar(1000))
  63. insert #a
  64. EXEC master..xp_cmdshell @Command
  65. select id, ouputtmp = s from #a
  66. -- drop table.
  67. drop table #a
  68. select @Command = 'del ' + @tempdir + @workfile
  69. EXEC master..xp_cmdshell @Command

Here, we have made a simple text file with a list of commands using your parameters and executed it. Gentle reminder, commectivity and destination path should be there.

Now, refer this link and try other commands http://www.nsftools.com/tips/MSFTP.htm by yourself.