How to Copy SQL Tables from One Database to Another?

Yatendra Singh    Yatendra Singh     Updated On - 15 May 2019

The need for copying tables from one database to another can appear in circumstances like maintenance, testing, demonstration, migration, transfer to a different instance, and more. Doing so is easy and can be done via multiple ways as SQL Server offers several methods for the same.
People experienced with SQL queries can execute the same operation in SQL Server with no problems at all, but those who hold less knowledge regarding SQL queries and Transact-SQL may find the task difficult.

In this article, we’ll guide you through how to copy tables from one database to another in SQL Server using different methods available and will also discuss a little on SQL Server database file corruption, and how to repair corrupt SQL database.

To copy tables, we need access to specific databases – the source server and the destination server. Here,

Source Database: TechForums19
Destination Database: TechForums20

Copying Tables Using Query

This method makes utilization of SELECT INTO query.

Select * into TechForums20.userforum.user from TechForums19.userforum.user

Above query copies only the table schema and data, and in case if you wish to copy objects, indexes, triggers, and constraints then, doing so is not possible with SQL queries/commands. We’ll discuss later in this article that how can you copy the rest of the database items.

Using SQL Server Management Studio

The second method for copying tables in SQL Server is the Export and Import wizard, available in SQL Server Management Studio. In this option, the user holds the choice either to import from the destination database or to export from the source database to transfer/copy the data.

Follow below steps to copy tables from one database to another in SQL Server:

  1. Open SQL Server Management Studio.
  2. Inside the object explorer, right-click on TechForums19 database > Tasks > select the Export Data command.
  3. In this step, specify the Server Name, Authentication method, and the Source database name, and click Next.
  4. select Source database name

    Note: For this guide, TechForums19 is the source database name as specified above. Change the source database name with yours and mention the server name & authentication method.

  5. In this step, specify the Destination database name and click Next.
  6. choose Destination database name

    Note: Again, mention the Server name and authentication method in this step as well.

  7. Select Copy data from one or more tables or views and click Next.
  8. Copy data from one or more tables or views

  9. Here, Select Source Tables and Views wizard will pop on-screen; select the Tables you want to copy from source database to destination database, as shown below and click Next.
  10. Select Source Tables and Views

    Note:
    To make sure, that the tables you selected will be created in the destination database, click on the Edit Mappings button and tick the Create destination table option.

    In case, if the tables contain the identity column, tick the Enable identity insert option, and click Ok button.

    If selected more than one table to copy to the destination database, click on Edit Mappings again, and check for all tables one after the other.

    Create destination table

  11. After checking all the tables via Edit Mappings, click Next in the Select Source Tables and Views.
  12. Save and Run wizard would open in this step, click on Next button.
  13. Save and Run wizard

  14. Click Finish.
  15. complete the process

    Note: Make sure to specify correct names of both Source & Destination databases, to transfer tables from one database to another.

    successfully move the tables

    Copying tables SQL Server using the SQL Server Management Studio is a quick way of completing the job, but it fails to transfer/copy the table’s indexes and keys.

    And if you want to copy the table indexes & keys, you’ll have to make use of the Generate Scripts method.

Using Generate Scripts

This method enables you to copy not only the table schema and data but also objects, indexes, trigger, constraints, keys, etc.

Go through the steps below to generate a script to fully copy tables from one database to another in SQL Server:

  1. Open SQL Server.
  2. Right-click on the database name > Tasks > Generate Scripts.
  3. Script Wizard would open, click on Next button.
  4. Select the Database you want to Generate Script for.
  5. Select the Object types and click on Next.
  6. Select the tables to copy.
  7. Choose the specific Output option for the script.
  8. Edit the database name with the name you want to execute the script for.
  9. Done.

What to do when the Database File Turns Corrupt or Damaged?

When you initiate any task or job that is associated with SQL Server database, there remains a slight risk of data loss or SQL Server database file corruption either due to wrong or incorrect execution or bad handling the database file. There are critical situations that result in frustration as getting data back from the inaccessible MDF/NDF database file becomes almost impossible.

In scenarios of SQL Server Database file corruption or damage, we recommend using a SQL recovery tool, Kernel for SQL Database Recovery.

This tool enables you to seamlessly, repair the corrupt, damaged, inaccessible MDF & NDF files with ease while maintaining the original file structure & properties. It also allows you to recover MDF files after ransomware attack.

SQL database recovery tool

Kernel for SQL Database Recovery provides the user with full control over database objects and sets no restriction over the file size.

Additionally, we’ve made it compatible with a wide range of Microsoft SQL Server versions starting from SQL Server 2000 to 2017, so no matter what version of SQL Server you’re running on your system, it’ll be ready to use once the installation is finished.

Wrap

Transferring or copying tables from one database to another in SQL Server is easy, but some methods are long and quite complex, and if something goes wrong during the process, both the data and the database file will be at high risk of file corruption & damage. Thus, to fight with such situations, Kernel for SQL Database Recovery, the ace third-party SQL recovery tool is available on the Internet.