Export table data from one SQL Server to another

后端 未结 12 668
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 07:29

I have two SQL Servers (both 2005 version).

I want to migrate several tables from one to another.

I have tried:

  • On source server I have righ

相关标签:
12条回答
  • 2020-12-02 07:53

    If the tables are already created using the scripts, then there is another way to copy the data is by using BCP command to copy all the data from your source server to your destination server

    To export the table data into a text file on source server:

    bcp <database name>.<schema name>.<table name> OUT C:\FILE.TXT -c -t -T -S <server_name[ \instance_name]> -U <username> -P <Password> 
    

    To import the table data from a text file on target server:

    bcp <database name>.<schema name>.<table name> IN C:\FILE.TXT -c -t -T -S <server_name[ \instance_name]> -U <username> -P <Password>
    
    0 讨论(0)
  • 2020-12-02 07:53

    Yet another option if you have it available: c# .net. In particular, the Microsoft.SqlServer.Management.Smo namespace.

    I use code similar to the following in a Script Component of one of my SSIS packages.

    var tableToTransfer = "someTable";
    var transferringTableSchema = "dbo";
    
    var srvSource = new Server("sourceServer");
    var dbSource = srvSource.Databases["sourceDB"];
    
    var srvDestination = new Server("destinationServer"); 
    var dbDestination = srvDestination.Databases["destinationDB"];
    
    var xfr = 
        new Transfer(dbSource) {
            DestinationServer = srvDestination.Name,
            DestinationDatabase = dbDestination.Name,
            CopyAllObjects = false,
            DestinationLoginSecure = true,
            DropDestinationObjectsFirst = true,
            CopyData = true
        };
    
    xfr.Options.ContinueScriptingOnError = false; 
    xfr.Options.WithDependencies = false; 
    
    xfr.ObjectList.Add(dbSource.Tables[tableToTransfer,transferringTableSchema]);
    xfr.TransferData();
    

    I think I had to explicitly search for and add the Microsoft.SqlServer.Smo library to the references. But outside of that, this has been working out for me.

    Update: The namespace and libraries were more complicated than I remembered.

    For libraries, add references to:

    • Microsoft.SqlServer.Smo.dll
    • Microsoft.SqlServer.SmoExtended.dll
    • Microsoft.SqlServer.ConnectionInfo.dll
    • Microsoft.SqlServer.Management.Sdk.Sfc.dll

    For the Namespaces, add:

    • Microsoft.SqlServer.Management.Common
    • Microsoft.SqlServer.Management.Smo
    0 讨论(0)
  • 2020-12-02 07:55

    Just for the kicks.

    Since I wasnt able to create linked server and since just connecting to production server was not enough to use INSERT INTO i did the following:

    • created a backup of production server database
    • restored the database on my test server
    • executed the insert into statements

    Its a backdoor solution, but since i had problems it worked for me.

    Since i have created empty tables using SCRIPT TABLE AS / CREATE in order to transfer all the keys and indexes I couldnt use SELECT INTO. SELECT INTO only works if the tables do not exist on the destination location but it does not copy keys and indexes, so you have to do that manualy. The downside of using INSERT INTO statement is that you have to manualy provide with all the column names, plus it might give you some problems if some foreign key constraints fail.

    Thanks to all anwsers, there are some great solutions but i have decided to accept marc_s anwser.

    0 讨论(0)
  • 2020-12-02 08:01

    For copying data from source to destination:

    use <DestinationDatabase>
    select * into <DestinationTable> from <SourceDataBase>.dbo.<SourceTable>
    
    0 讨论(0)
  • 2020-12-02 08:04

    Try this:

    1. create your table on the target server using your scripts from the Script Table As / Create Script step

    2. on the target server, you can then issue a T-SQL statement:

      INSERT INTO dbo.YourTableNameHere
         SELECT *
         FROM [SourceServer].[SourceDatabase].dbo.YourTableNameHere
      

    This should work just fine.

    0 讨论(0)
  • 2020-12-02 08:04

    You can't choose a source/destination server.

    If the databases are on the same server you can do this:

    If the columns of the table are equal (including order!) then you can do this:

    INSERT INTO [destination database].[dbo].[destination table]
    SELECT *
    FROM [source database].[dbo].[source table]
    

    If you want to do this once you can backup/restore the source database. If you need to do this more often I recommend you start a SSIS project where you define source database (there you can choose any connection on any server) and create a project where you move your data there. See more information here: http://msdn.microsoft.com/en-us/library/ms169917%28v=sql.105%29.aspx

    0 讨论(0)
提交回复
热议问题