Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 存储过程中的ftp xp_cmdshell脚本_Sql Server_Ftp_Sql Server 2008 R2 - Fatal编程技术网

Sql server 存储过程中的ftp xp_cmdshell脚本

Sql server 存储过程中的ftp xp_cmdshell脚本,sql-server,ftp,sql-server-2008-r2,Sql Server,Ftp,Sql Server 2008 R2,我正在构建一个存储过程,将数据从SQL Server传输到远程FTP服务器。 我使用以下查询将表数据写入一个文件 exec xp_cmdshell "bcp master.dbo.table out c:\temp\bcp_outputFile.txt -S(local)\SQLEXPRESS -T -c" 但是,我正在努力构建一个脚本,将该文件发送到FTP服务器 使用SSI不是一个选项。必须使用xp\u cmdshell命令完成此操作 有什么想法吗 谢谢使用下面提到的步骤将文件上载到ftp服

我正在构建一个存储过程,将数据从SQL Server传输到远程FTP服务器。 我使用以下查询将表数据写入一个文件

exec xp_cmdshell "bcp master.dbo.table out c:\temp\bcp_outputFile.txt -S(local)\SQLEXPRESS -T -c"
但是,我正在努力构建一个脚本,将该文件发送到FTP服务器

使用SSI不是一个选项。必须使用xp\u cmdshell命令完成此操作

有什么想法吗


谢谢

使用下面提到的步骤将文件上载到ftp服务器。 在这里的过程中,我硬编码的ftp详细信息,你可以改变它

create procedure [dbo].[uploadfile_ftp]
    @SourcePath varchar(128),
    @SourceFiles varchar(128),
    @DestPath varchar(128)
AS
Begin

DECLARE @FTPServer varchar(128)
DECLARE @FTPUser varchar(128)
DECLARE @FTPPwd varchar(128)
DECLARE @FTPMode varchar(10)

-- FTP attributes.
SET @FTPServer = 'ftp url'
SET @FTPUser = 'ftp username'
SET @FTPPwd = 'ftp password'
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
print @cmd;

select @cmd = 'echo ' + @FTPUser + '>> ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
print @cmd;

select @cmd = 'echo ' + @FTPPwd + '>> ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
print @cmd;

select @cmd = 'echo ' + 'prompt ' + ' >> ' + @tempdir + @workfile
EXEC master..xp_cmdshell @cmd
print @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 ' + 'mput ' + @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
print @cmd;
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

End