Prashanth Jayaram

Six different methods to copy tables between databases in SQL Server

November 16, 2018 by

In this article, you’ll learn the key skills that you need to copy tables between SQL Server instances including both on-premises and cloud SQL databases. In this article, I’ll walk-through several ways of copying a table(s) between SQL databases, helping you to see the benefits and trade-offs of each option.

Introduction

Before we begin the article, though, let’s go over the objectives of the article. We then move on to the overview of each module or methods. In this guide, we briefly discuss several aspects of SQL Server’s available built-in options, as well as show you a few PowerShell and 3rd party tools can be used to copy SQL tables between the databases and between the instances as well. At the beginning of each method, I’ve given you enough information that the following modules. We follow this module up with several modules, each of which is dedicated to specific methods.

Objectives:

  1. Introduction
  2. Discuss various methods to copy tables
    • Using .Net class library to copy tables with PowerShell
    • Using Import-and-Export Wizard
    • Using sqlpackage.exe – Extract and Publish method
    • Using Generate Scripts wizard in SSMS ( SQL Server Management Studio)
    • Using INSERT INTO SQL statement
  3. And more…

Get started

In SQL Server, copying tables between the databases of the same SQL instances are relatively easier than copying the data between the remote servers. To minimize the work-load on the production database, it is always recommended to restore the database from the backup to the new database and then use the best methods to copy the data to the target database. Again, this depends on the number of tables, size, and available space. If the size of the table(s) is more than 50% of the total size of the database than the backup-and-restore method is a recommended option.

In some cases, you might have to copy a few very large table(s), and then you may probably end-up in moving the table(s) to separate file-groups and perform a partial backup-and-restore method to copy the data. You can refer to the article Database Filegroup(s) and Piecemeal restores in SQL Server for more information.

You can also use third-party tools to perform an object level restore from a backup file.

SqlBulkCopy object class for Data copy with PowerShell

PowerShell is always my first choice for any administrative task. Net provides a SqlBulkCopy class library to bulk load the table(s) into the database.

You can refer to the article 6 methods to write PowerShell output to a SQL Server table to get more information about .Net class libraries.

PowerShell script

The following PoSH script creates a function named Get-SQLTable. The function has several mandatory parameters.

The $tables array variable is used to assign the list of the table(s) to be copied to the target database

Let us invoke the Get-SQLTable function with the below mentioned parameters to copy the tables from Adventureworks2016 database on ‘HQDBT01’ to Adentureworks2012 database on hqdbt01/sql2017’ instance.

The output shows the tables OPERATION and OPERATION_DETAIL copied to the target instance.

SSMS Import-and-Export Wizard

Let’s take a look at the Import-and-Export Wizard. The interface is very similar to all other wizards, allow you to easily step through a process, and to execute the data copy process with writing very little or no code. To do that, we have very few options that we can do within the wizard. However, for this, for importing and exporting data from one source into another, this is really an excellent tool. If you want to do almost any kind of transformations, then you don’t want use this tool, you may need to use Visual Studio Data Tools (VSDT), and do a data flow.

So let’s get started. The first thing is to open Microsoft SQL Server Management Studio (SSMS). And we’re going use to AdventureWorks2016 database, and we’re going to move it over to another instance of SQL.

  • Open the Object Explorer, locate the database, right-click and select Tasks and choose Export Data option.
  • Now the data source, if I pull this down, you’ll see the different sources that we can use. We’re going to use SQL Native Client 11.0, the SQL provider.
  • Next, Server name, it is recommended practice to pull server name and database in the import and export wizard using the selection drop-down list.

  • Now the destination selection, again pull down SQL provider, Server name and Database from a drop-down list rather than typing it. And we’ll go Next

  • In Select Source Tables and Views, select the objects to copy to the destination or you could write a query. But here we’re just going to copy the data. In this case, let’s bring in the dbo.Cities and Person.Address.
  • Click Next

  • We’re ready to run the copy job. Let us choose Run immediately and Click Next

  • We can see a summary of the action that we are going to perform using the wizard
  • Click Finish to execute the job steps.

  • After successful execution of the job, we can validate and review the output.

Using sqlpackage.exe – Extract and Publish method

Sqlpackage is command-line utility that automate “Schema-and-data” extraction process and publish the generated file into a target database. The SqlPackage.exe command-line utility is an in-house component of SQL Server Data Tools (SSDT).

Using Sqlpackage.exe, it’s possible to extract the schema-and-data, and then publish only those listed table(s) data. In the process, the objects such as Stored Procedures, Functions, etc are extracted into the .dacpac file, but they’re excluded when publishing the content into the target database.

It is possible to list the tables. On specifying individual tables, you need to first specify /p:ExtractAllTableData=False and then add /p:TableData property to specify each table in the form of Schema.Table.

The following example uses the property /p:TableData for three tables. You can see that the tables are referred in the form of dbo.orders.

/p:TableData=dbo.Orders
/p:TableData=Orders.Orders
/p:TableData=Person.Address

SqlPackage /Action:Extract /SourceDatabaseName:Adventureworks2016 /SourceServerName:HQDBT01 /TargetFile:F:\PowerSQL\smartbackup\AdventureWorks2016.dacpac /p:IgnoreExtendedProperties=True /p:ExtractAllTableData=FALSE /p:TableData=dbo.Cities /p:TableData=dbo.citiesDemo

Let’s prepare the script to automate the extract-and-publish process

  1. Set the ENVIRONMENT variable. The file may be found in other directories depend on the SSDT installation. In this case, SqlPackage.exe is found in C:\Program Files (x86)\Microsoft SQL Server\140\DAC\bin\ folder
  2. Prepare the Input values
    1. Backup Directory
    2. Source Database
    3. Source SQL Server instance
    4. Target database
    5. Target SQL Server instance
  3. Run the SqlPackage.exe with an extract action on the source SQL instance
  4. Run T-SQL to find the existence of the target database
  5. Run the SqlPackage.exe with a publish action on the target SQL instance

Output:

In the output, you can see that the table dbo.orders and dbo.address tables are processed.

Generate Scripts using SQL Server Management Studio

In this section, we’ll discuss another way to generate “schema and data” for SQL Server databases objects.

Let’s see the steps to generate a SQL Script that includes both “Schema and Data”

  1. Connect the SQL Server instance
  2. Open the Object Explorer and locate the database
  3. Right-click the database, select Tasks, and then click on Generate Scripts…. After that, the Script Wizard opens. Click on “Next”.

  4. On Choose Object page, enable the Select specific database objects option. Select the intended objects and Click Next.

  5. In Set Scripting Options, Select the Output Type and Click Advanced button. In this case, the output type re-directed to query window.

  6. In the Advanced Scripting Options, select “Schema and Data” from the drop-down list and Click OK.

  7. Next, the Summary page details the outlines of the entire process. Click Next

  8. Now, Save or Publish Scripts page shows the progress of the entire process. You can monitor the status of the entire schema and data generation process.

INSERT INTO SQL

This is also an option to clone the table from database to another.

You can refer to the article Overview of the SQL Insert for more information.

Summary

So far, we’ve discussed various methods to copy the tables across SQL Server databases. It is evident that restoring a couple of tables from a backup can be time and space consuming process. It is up to your environment to follow any of the aforementioned steps to copy the tables in SQL Server. There is no standard/recommended way to copy a table between the databases but there are many possible approaches that you can use to fit your needs


Prashanth Jayaram
Maintenance, Migration

About Prashanth Jayaram

I’m a Database technologist having 11+ years of rich, hands-on experience on Database technologies. I am Microsoft Certified Professional and backed with a Degree in Master of Computer Application. My specialty lies in designing & implementing High availability solutions and cross-platform DB Migration. The technologies currently working on are SQL Server, PowerShell, Oracle and MongoDB. View all posts by Prashanth Jayaram

168 Views