(供应商环境阻止我们将SSIS用于此解决方案,因此我们使用T-SQL完成FTP工作。)
FTP上传文件有很多代码示例但是我无法通过"无效命令"当我运行脚本时。有没有人有任何想法?
SET QUOTED_IDENTIFIER ON
-- sp_configure
DECLARE
@FTPServer varchar(128) ,
@FTPUser varchar(128) ,
@FTPPWD varchar(128) ,
@FTPPath varchar(128) ,
@FTPFileName varchar(128) ,
@SourcePath varchar(128) ,
@SourceFile varchar(128) ,
@workdir varchar(128),
@workfilename varchar(128),
@cmd varchar(1000)
SELECT @workfilename = 'ftpcmd.txt'
SET @workdir = 'E:\Extract\'
SET @FTPServer = 'ftp.blah.com'
SET @FTPUser = 'user1'
SET @FTPPWD = 'XXXXXXX'
SET @SourcePath = 'E:\ForOracleExtract\'
SET @SourceFile = 'test.txt'
SET @FTPPath = 'ftp://ftp.blah.com/directory/incoming/'
SET @FTPFileName = 'test.txt'
-- deal with special characters for echo commands
select @FTPServer = replace(replace(replace(@FTPServer, '|', '^|'),'<','^<'),'>','^>')
select @FTPUser = replace(replace(replace(@FTPUser, '|', '^|'),'<','^<'),'>','^>')
select @FTPPWD = replace(replace(replace(@FTPPWD, '|', '^|'),'<','^<'),'>','^>')
select @FTPPath = replace(replace(replace(@FTPPath, '|', '^|'),'<','^<'),'>','^>')
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 ' + 'ascii >> ' + @workdir + @workfilename
exec master..xp_cmdshell @cmd
SELECT @cmd = 'echo ' + 'put ' + @SourcePath + @SourceFile + ' ' + @FTPPath + @FTPFileName + ' >> ' + @workdir + @workfilename
EXEC master..xp_cmdshell @cmd
SELECT @cmd = 'echo ' + 'quit' + ' >> ' + @workdir + @workfilename
EXEC master..xp_cmdshell @cmd
SELECT @cmd = 'ftp -s:' + @workdir + @workfilename
-- Executing final step
EXEC master..xp_cmdshell @cmd
GO