我想使用SQL Server脚本生成一个文件,然后将该文件传输到ftp服务器。
当配置的加密是"使用普通的ftp"时,以下脚本非常有效。不幸的是,如果加密是"需要在TLS"上显式ftp,那么相同的脚本不起作用。
任何人都可以知道如何修改脚本(我应该添加什么)让它工作?
提前致谢
declare @bcpcmd varchar (4000), @file varchar( 20)
set @file = 'File_' + cast (year( getdate()) as varchar ) + '_'+ right('0' + cast(month (getdate()) as varchar),2 )+ '_' + right('0'+ cast( day(getdate ()) as varchar),2 ) + '_' + right('0' + cast(datepart (Hour, getdate()) as varchar ),2)+ '_' + right('0' + cast (datepart( minute,getdate ()) as varchar), 2)
set @bcpcmd = 'bcp "select * from mydb.dbo.customer" queryout C:\Web\'+@file+ '.csv'+' -t^; -T -c'
EXEC master ..xp_cmdshell @bcpcmd
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 = 'ftp.xxxx.com'
SET @FTPUser = 'myLogin'
SET @FTPPwd = 'myPwd'
SET @SourcePath = 'C:\Web'
SET @SourceFiles = @file+'.csv'
SET @DestPath = 'Sources' -- Destination path. Blank for root directory.
SET @FTPMode = 'binary' -- ascii, binary or blank for default.
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