我每天晚上都有一份工作要在我的服务器上生成一个文件。
declare @cmd varchar (4000)
set @cmd = 'bcp "select * from databasename.dbo.viewname" queryout C:\FolderName\' + convert(varchar, getdate(), 112) + '_'+ substring(convert(varchar, getdate(), 114),1,2)+ substring(convert(varchar, getdate(), 114),4,2) + '_FileName.txt -t^; -T -c'
EXEC master..xp_cmdshell @cmd
现在,我想直接将文件生成到ftp站点。
因为似乎不可能仅使用bcp命令,所以我使用以下脚本来执行此操作。 (我在2个不同的ftp站点上测试)
DECLARE @FTPServer varchar(128)
DECLARE @FTPUser varchar(128)
DECLARE @FTPPwd varchar(128)
DECLARE @SourcePath varchar(128)
DECLARE @SourceFiles varchar(128)
DECLARE @DestPath varchar(128)
DECLARE @FTPMode varchar(10)
-- FTP attributes.
SET @FTPServer = 'MyftpDomain'
SET @FTPUser = 'Mylogin'
SET @FTPPwd = 'MyPwd'
SET @SourcePath = 'C:\ftp'
SET @SourceFiles = 'test.txt'
SET @DestPath = 'files'
SET @FTPMode = 'binary'
DECLARE @cmd varchar(1000)
DECLARE @workfile varchar(128)
DECLARE @nowstr varchar(25)
-- Get the %TEMP% environment variable.
DECLARE @tempdir varchar(128)
CREATE TABLE #tempvartable(info VARCHAR(1000))
INSERT #tempvartable EXEC master..xp_cmdshell 'echo %temp%'
SET @tempdir = (SELECT top 1 info FROM #tempvartable)
IF RIGHT(@tempdir, 1) <> '\' SET @tempdir = @tempdir + '\'
DROP TABLE #tempvartable
-- Generate @workfile
SET @nowstr = replace(replace(convert(varchar(30), GETDATE(), 121), ' ', '_'), ':', '-')
SET @workfile = 'FTP_SPID' + convert(varchar(128), @@spid) + '_' + @nowstr + '.txt'
-- Deal with special chars for echo commands.
select @FTPServer = replace(replace(replace(@FTPServer, '|', '^|'),'<','^<'),'>','^>')
select @FTPUser = replace(replace(replace(@FTPUser, '|', '^|'),'<','^<'),'>','^>')
select @FTPPwd = replace(replace(replace(@FTPPwd, '|', '^|'),'<','^<'),'>','^>')
select @DestPath = replace(replace(replace(@DestPath, '|', '^|'),'<','^<'),'>','^>')
IF RIGHT(@SourcePath, 1) <> '\' SET @SourcePath = @SourcePath + '\'
-- Build the FTP script file.
select @cmd = 'echo ' + 'open ' + @FTPServer + ' > ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
select @cmd = 'echo ' + @FTPUser + '>> ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
select @cmd = 'echo ' + @FTPPwd + '>> ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
select @cmd = 'echo ' + 'prompt ' + ' >> ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
IF LEN(@FTPMode) > 0
BEGIN
select @cmd = 'echo ' + @FTPMode + ' >> ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
END
IF LEN(@DestPath) > 0
BEGIN
select @cmd = 'echo ' + 'cd ' + @DestPath + ' >> ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
END
select @cmd = 'echo ' + 'put ' + @SourcePath + @SourceFiles + ' >> ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
select @cmd = 'echo ' + 'quit' + ' >> ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
-- Execute the FTP command via script file.
select @cmd = 'ftp -s:' + @tempdir + @workfile
create table #a (id int identity(1,1), s varchar(1000))
insert #a
EXEC master..xp_cmdshell @cmd
select id, ouputtmp = s from #a
-- Clean up.
drop table #a
select @cmd = 'del ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
1 /我首先尝试从本地电脑上做。所以我生成文件,然后执行脚本,以便将文件放入ftp。
它有效!!!!
1.1 /我试图直接从cmd窗口(dos)
进行+ open : OK
+ login : OK
+ password : OK
+ bin : OK
+ put : OK
它有效!!!
2 / 2.1 /我试图在另一个ftp网站上做同样的事情,它也可以从脚本或cmd窗口(dos)工作!!!
当我尝试从我拥有sqlserver许可证的远程服务器执行此操作时出现问题:
所以,我尝试从我的MS Sql Server的Microsoft Azure服务器中执行相同的操作。它不适用于没有2个方法(脚本和cmd窗口)的两个ftp站点
当我尝试在第一个ftp上使用脚本放置文件时它会生成以下错误:
Illegal PORT command.
并使用cmd窗口生成:
500 Illegal PORT command.
425 Use PORT or PASV first
请注意,在第一个ftp上,活动传输模式已启用
在启用默认传输模式的第二个ftp上, 当我尝试使用脚本放置文件时,脚本运行时不停止(从未完成)。
使用cmd窗口,当我输入put commande时,我得到:
200 port command successfull
150 opening data channel for file upload to server of "filepath"
任何人都可以帮助我理解问题的根源,我该如何避免它。
提前致谢