sql server - Upload file to ftp site in MSSQL -
i trying upload files ftp site in mssql. after executed query, in result, showing invalid command, interactive mode off,not connected, , output null. appreciate if give me guide on it. please see query below: query result screen shot
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) -- %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 #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 special chars 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 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 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 #a -- clean up. drop table #a select @cmd = 'del ' + @tempdir + @workfile exec master..xp_cmdshell @cmd
Comments
Post a Comment