Export table data from one SQL Server to another

后端 未结 12 748
伪装坚强ぢ
伪装坚强ぢ 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

    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

提交回复
热议问题