使用存储过程和大容量复制程序(bcp)实用程序通过FTP将文件从一台SQL Server移动到另一台SQL Server

使用存储过程和大容量复制程序(bcp)实用程序通过FTP将文件从一台SQL Server移动到另一台SQL Server,sql,sql-server,stored-procedures,ftp,bcp,Sql,Sql Server,Stored Procedures,Ftp,Bcp,以下是我试图编写的过程: 首先,我需要在SQL Server 1中生成一个文件,其中包含一些可以从同一服务器查询的数据 然后我需要将文件从SQLServer1移动到FTP服务器,从那里我需要从SQLServer2获取文件的详细信息 在bcp实用程序的帮助下,我该如何做?在sql server中创建一个包含查询数据的文件 DECLARE @FileName varchar(1000),@bcpCommand varchar(1000), @FileGenerateP

以下是我试图编写的过程:

  • 首先,我需要在SQL Server 1中生成一个文件,其中包含一些可以从同一服务器查询的数据

  • 然后我需要将文件从SQLServer1移动到FTP服务器,从那里我需要从SQLServer2获取文件的详细信息


  • bcp
    实用程序的帮助下,我该如何做?

    在sql server中创建一个包含查询数据的文件

        DECLARE @FileName varchar(1000),@bcpCommand varchar(1000),
                @FileGeneratePath varchar(100)
        DECLARE @exe_path4 VARCHAR(200) = ' cd C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn & ';
    
        Set @FileGeneratePath = '\\192.168.XX.XXX\Files\'
        Set @FileName = REPLACE('SampleFile'+ CONVERT(varchar(10),GETDATE(),100)+'.xls',' ','') --'\\192.168.29.111\Files\authors'+ Convert(varchar(40),Getdate(),121) + '.csv'
    
        SET @bcpCommand = @exe_path4 +' bcp.exe "SELECT top 10 * from [192.168.XX.XXX].DATABASE.dbo.FamilyAccountDetails where Convert(varchar(10),LastUpdatedon,100)  > Convert(varchar(10),Getdate()-30,100)" queryout "' 
        SET @bcpCommand = @bcpCommand + @FileGeneratePath + @FileName + '" -c -t"|" -Uusername -Ppassword -S192.168.XX.XXX'
    
        EXEC master..xp_cmdshell @bcpCommand, no_output
    
    exe_path4是bcp exe的路径。 有关bcp实用程序的更多详细信息。而且

    xp_cmdshell是一个扩展存储过程,它允许您通过SQL语句执行操作系统命令。扩展存储过程是用C++语言编写的程序,然后附加到SQL Server实例中。一旦附加,它们就可以像常规存储过程一样被引用

    xp\u cmdshell中的
    no\u输出
    类似于
    set nocount on

    将文件从sql server移动到ftp的存储过程

    在下面的存储过程中,我创建了一个文本文件,在命令提示符下执行,将文件移动到ftp,反之亦然。这两个文件都是在SQL Server临时文件夹中生成的,而不是在本地临时文件夹中生成的。您还可以为文件生成指定特定文件夹

    /*     
        Exec spPullFileToFTP
        @FTPServer = '192.168.YY.YYY' ,
        @FTPUser = 'username' ,
        @FTPPWD = 'password' ,
        @SourcePath = '\\192.168.YY.YYY\Files\' ,
        @SourceFiles = 'SampleFileJan16201.xls' ,
        @DestPath = 'FileToCopy/New/' 
    
            */
    Create Procedure spPullFileToFTP
     @FTPServer varchar(128),
     @FTPUser varchar(128),
     @FTPPwd varchar(128),
     @SourcePath varchar(128),
     @SourceFiles varchar(128),
     @DestPath varchar(128),
     @FTPMode varchar(10)=''
    as
    
    Set Nocount On
    
    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 ' + 'mput ' + @SourcePath + @SourceFiles + ' >> ' + @tempdir + @workfile
    EXEC master..xp_cmdshell @cmd
    print @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
    go
    
    将文件从ftp移动到SQL Server的存储过程

    /*     
        exec spPullFileFromFTP  
        @FTPServer = '192.168.YY.YYY' ,
        @FTPUser = '11111' ,
        @FTPPWD = 'FtpPassword' ,
        @SourcePath = '/FileToCopy/Sample/' ,
        @SourceFiles = 'SampleFileJan12201.xls' ,
        @DestPath = '\\192.168.XX.XXX\Files\New\' 
    
    
    
            */
    Create procedure spPullFileFromFTP
     @FTPServer varchar(128),
     @FTPUser varchar(128),
     @FTPPwd varchar(128),
     @SourcePath varchar(128),
     @SourceFiles varchar(128),
     @DestPath varchar(128),
     @FTPMode varchar(10)=''
    as
    Begin
    
        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 @SourcePath = replace(replace(replace(@SourcePath, '|', '^|'),'<','^<'),'>','^>')
        IF RIGHT(@DestPath, 1) = '\' SET @DestPath = LEFT(@DestPath, LEN(@DestPath)-1)
    
        -- 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
    
        select @cmd = 'echo ' + 'lcd ' + @DestPath + ' >> ' + @tempdir + @workfile
        EXEC master..xp_cmdshell @cmd
    
        IF LEN(@SourcePath) > 0
        BEGIN
            select @cmd = 'echo ' + 'cd ' + @SourcePath + ' >> ' + @tempdir + @workfile
            EXEC master..xp_cmdshell @cmd
        END
    
        select @cmd = 'echo ' + 'mget '  + @SourceFiles + ' >> ' + @tempdir + @workfile
        --select @cmd = 'echo ' + 'mget ' + @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
    
    End
    
    如果对命令有任何疑问,请

    从文件到数据库表

            Declare @SQL varchar(500)
            set @SQL = 'BULK INSERT  BGL_CRM_FamilyAccountDetails  FROM '+''''+'//192.168.YY.XXX/Files/New/'+ @FileName +''''+ 
            ' WITH (DATAFILETYPE =' +''''+'char' +''''+', FIELDTERMINATOR = '+''''+'|'+''''+', ROWTERMINATOR = '+''''+'\n'+''''+')'                
    
            EXEC(@SQL)   
    
    open 192.168.YY.YYY
    Username
    Password
    prompt  
    binary 
    lcd \\192.168.XX.XXX\Files\New 
    cd /FileToCopy/AdvisoryPortal/ 
    mget SampleFileJan12201.xls 
    quit 
    
            Declare @SQL varchar(500)
            set @SQL = 'BULK INSERT  BGL_CRM_FamilyAccountDetails  FROM '+''''+'//192.168.YY.XXX/Files/New/'+ @FileName +''''+ 
            ' WITH (DATAFILETYPE =' +''''+'char' +''''+', FIELDTERMINATOR = '+''''+'|'+''''+', ROWTERMINATOR = '+''''+'\n'+''''+')'                
    
            EXEC(@SQL)