将特定数据从一台服务器传输到另一台服

时间:2018-05-15 10:47:04

标签: sql sql-server tsql transfer

我有2个不同的sql server(2个不同的数据库)。 这两个服务器具有相同的表。 现在我想从Server 1的Person表转移到Server 2的Person表,只有ID在1.000到50.000之间的记录。 我怎么能以最简单的方式做到这一点?

尝试使用Generate Scripts,但是没有选项可以只选择那些ID,脚本会传输所有记录。 尝试在服务器1上使用SELECT语句并将数据导出为CSV,然后在服务器2上导入CSV文件,但由于datetimeoffset字段显然存在一些问题......

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我在2个域中的数据无法通过网络看到对方,我必须得到一些日期,而不是所有数据并将其移动到"其他"服务器

我编写了一个脚本,该脚本从文件组中获取所有数据,并创建了该数据的转储以及加载数据的脚本。

过了一会儿,他们也开始将数据转储到存档中,以便将数据保留为" csv"无论使用何种数据库,都可以随时恢复版本" 7年"从现在起......

无论如何,它只是一个大的"打印"使用BCP在服务器之间移动大量数据的语句。您可以调整它以执行您喜欢的操作,只需更改一下查询,文件顶部包含"控制"变量

/*******************************************************************
this script will generate thebcp out commands for all data from the 
users current connected database. The this script will only work if
both databases have the same ddl version, meaning same tables, same
columns same data definitions.
*******************************************************************/




SET NOCOUNT ON 
GO 
DECLARE  @Path                  nvarchar(2000)  = 'f:\export\'  -- storage location for bcp dump (needs to have lots of space!)
       , @Batchsize             nvarchar(40)    = '1000000'     -- COMMIT EVERY n RECORDS 
       , @Xmlformat             bit             = 0             -- 1 for yes to xml format, 0 for not xml
       , @SourceServerinstance  nvarchar(200)   = 'localhost'-- SQL Server \ Instance name 
       , @Security              nvarchar(800)   = ' -T '        -- options are -T (trusted), -Uloginid -Ploginpassword  
       , @GenerateDump          bit             = 0             -- 0 for storing data to disk, not 1 for loading from disk
       , @FileGroup             sysname         = 'Data';       -- Table filegroup that we are intrested in

--> set output to text and execute the query, then copy the generated commands, validate and execucte them                   
--------------------------------Do not edit below this line-----------------------------------------------------------------
DECLARE @filter TABLE(TABLE_NAME sysname)  
INSERT INTO @filter (TABLE_NAME)
SELECT o.name
  FROM sys.indexes as i 
  JOIN sys.objects as o on o.object_id = i.object_id
 WHERE i.data_space_id = FILEGROUP_ID(@FileGroup) 
   AND i.type_desc ='CLUSTERED'   
   and o.name not like 'sys%'   
 order by 1      

if(@GenerateDump=0)
begin

    --BCP-OUT TABLES 
    SELECT  'bcp "' + QUOTENAME( TABLE_CATALOG ) + '.' + QUOTENAME( TABLE_SCHEMA ) 
    + '.' + QUOTENAME( TABLE_NAME ) + '" out "' + @path + '' + TABLE_NAME + '.dat" -q -b"' 
    + @batchsize + '" -e"' + @path + 'Error_' + TABLE_NAME + '.err" -n -CRAW -o"' + @path + '' 
    + TABLE_NAME + '.out"  -S"' + @SourceServerinstance + '" ' + @security + '' 
    FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME IN (SELECT TABLE_NAME FROM @filter)

    if(@Xmlformat=0)
        begin
            print 'REM CREATE NON-XML FORMAT FILE '
            SELECT  'bcp "' + QUOTENAME( TABLE_CATALOG ) + '.' + QUOTENAME( TABLE_SCHEMA ) + '.'+ 
             QUOTENAME( TABLE_NAME ) + '" format nul -n -CRAW -f "' + @path + '' 
            + TABLE_NAME + '.fmt"  -S"' + @SourceServerinstance + '" ' + @security + '' 
            FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'  AND TABLE_NAME IN (SELECT TABLE_NAME FROM @filter)
        end
    else
        begin
            PRINT 'REM XML FORMAT FILE' 
            SELECT  'bcp "' +QUOTENAME( TABLE_CATALOG ) + '.' + QUOTENAME( TABLE_SCHEMA ) 
            + '.' + QUOTENAME( TABLE_NAME ) + '" format nul -x -n -CRAW -f "' 
            + @path + '' + TABLE_NAME + '.xml"  -S"' + @SourceServerinstance + '" ' + @security + '' 
            FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'  AND TABLE_NAME IN (SELECT TABLE_NAME FROM @filter) 
        end
end
else
begin
    print '--Make sure you backup your database first'
    --GENERATE CONSTRAINT NO CHECK 
    PRINT '--NO CHECK CONSTRAINTS' 
    SELECT 'ALTER TABLE ' + QUOTENAME(TABLE_SCHEMA)+'.'+QUOTENAME( TABLE_NAME ) + ' NOCHECK CONSTRAINT ' +  QUOTENAME( CONSTRAINT_NAME ) 
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME IN (SELECT TABLE_NAME FROM @filter)

    PRINT '--DISABLE TRIGGERS' 
    SELECT 'ALTER TABLE ' + QUOTENAME(TABLE_SCHEMA)+'.'+QUOTENAME( TABLE_NAME ) + ' DISABLE TRIGGER ALL' 
    FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME IN (SELECT TABLE_NAME FROM @filter)
    --TRUNCATE TABLE 
    SELECT 'TRUNCATE TABLE ' +QUOTENAME(TABLE_SCHEMA)+'.'+QUOTENAME( TABLE_NAME ) + ' 
    GO ' 
    FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME IN (SELECT TABLE_NAME FROM @filter)

    --BULK INSERT 
    SELECT DISTINCT 'BULK INSERT ' + QUOTENAME(TABLE_CATALOG) + '.' 
    + QUOTENAME( TABLE_SCHEMA ) + '.' + QUOTENAME( TABLE_NAME ) + ' 
    FROM ''' + @path + '' + TABLE_NAME + '.Dat'' 
    WITH (FORMATFILE = ''' + @path + '' + TABLE_NAME + '.FMT'', 
    BATCHSIZE = ' + @batchsize + ', 
    ERRORFILE = ''' + @path + 'BI_' + TABLE_NAME + '.ERR'',         
    TABLOCK); 
    GO ' 
    FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME IN (SELECT TABLE_NAME FROM @filter)

    --GENERATE CONSTRAINT CHECK CONSTRAINT TO VERIFY DATA AFTER LOAD 
    PRINT '--CHECK CONSTRAINT' 


    SELECT 'ALTER TABLE ' + QUOTENAME(TABLE_SCHEMA)+'.'+QUOTENAME( TABLE_NAME ) + ' CHECK CONSTRAINT ' 
    FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME IN (SELECT TABLE_NAME FROM @filter)

    SELECT 'ALTER TABLE ' + QUOTENAME(TABLE_SCHEMA)+'.'+ QUOTENAME( TABLE_NAME ) + ' ENABLE TRIGGER ALL' 
    FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME IN (SELECT TABLE_NAME FROM @filter)

    end

答案 1 :(得分:0)

最后,最简单的方法是使用在两个服务器之间创建链接服务器,并执行我的查询,从两个服务器中获取数据,并从第一台服务器中排除ID。

感谢大家的答复。