简体   繁体   中英

Transfer specific data from one server to another

I have 2 different sql servers (2 different databases). The 2 servers have the same tables. Now I want to transfer from Server 1's Person table to Server 2's Person table only the records with ID between 1.000 and 50.000. How could I do it in the easiest way ?

Tried with Generate Scripts, but there isn't an option to select just those IDs, the script transfers all the records. Tried by using a SELECT statement on Server 1 and exporting the data as CSV, then importing the CSV file on Server 2, but apparently there are some problems because of the datetimeoffset fields...

I had the same problem where I had data in 2 domains that could not see each other over the network, I had to get some date, not all data and move that to the "other" server.

I wrote a script that took all data from a file group and created a dump of that data as well as the script to load the data.

A little later they also started to dump data out to archive for data that needed to be retained as the "csv" version can always be restored, regardless of the database used "7 years" from now...

Anyway, it's just a big "print" statement that uses BCP to move massive amounts of data between servers. You can tweak it to do what you like, just alter the query a bit, the top of the file contains the "control" variables.

/*******************************************************************
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

At the end the easiest way was to use create a linked server between the 2 and execute my queries taking data from both the servers and excluding the IDs from the first server.

Thanks to everyone for the responses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM