In some cases, as a DBA, you are requested to copy the schema and the content of specific tables from a database to another one in the same instance or in a different SQL instance, such as copying specific tables from a production database to a DEV one for testing or troubleshooting purposes.
在某些情况下,作为DBA,要求您将模式和特定表的内容从数据库复制到同一实例或不同SQL实例中的另一数据库,例如将特定表从生产数据库复制到DEV。一种用于测试或故障排除的目的。
SQL Server offers a lot of methods that can be used to perform table’s data and schema copy process. In order to go through each one of these methods, we will consider the below scenario:
SQL Server提供了许多可用于执行表的数据和架构复制过程的方法。 为了遍历这些方法中的每一种,我们将考虑以下情形:
The tables that will be copied from the source database to the destination one are: Department, Employee, EmployeeDepartmentHistory and EmployeePayHistory under the HumanResources schema.
将要从源数据库复制到目标数据库的表是在HumanResources模式下的Department , Employee , EmployeeDepartmentHistory和EmployeePayHistory 。
Tables Copying Methods:
表格复制方法:
Using SELECT INTO Query
使用SELECT INTO查询
In order to copy our tables from the AdventureWorks2018 database to the SQLShackDemo one, we can use the Select into SQL statement. This statement will create the tables in the destination database first, then it will copy the data to these tables. If you manage to copy the database objects such as the indexes and constraints, you need to generate script for it individually, after that you need to apply the scripts to the destination database.
为了将表从AdventureWorks2018数据库复制到SQLShackDemo之一,我们可以使用Select into SQL语句。 该语句将首先在目标数据库中创建表,然后将数据复制到这些表中。 如果您设法复制数据库对象(例如索引和约束),则需要为其单独生成脚本,然后再将脚本应用于目标数据库。
In our example, to copy the Department, Employee, EmployeeDepartmentHistory and EmployeePayHistory tables under the HumanResources schema from the AdventureWorks2018 database to the SQLShackDemo database, we will run the below script:
在我们的示例中,要将HumanResources模式下的Department , Employee , EmployeeDepartmentHistory和EmployeePayHistory表从AdventureWorks2018数据库复制到SQLShackDemo数据库,我们将运行以下脚本:
Create schema HumanResources
Go
Select * into SQLShackDemo.HumanResources.Department from
AdventureWorks2018.HumanResources.Department
Select * into SQLShackDemo.HumanResources.Employee from
AdventureWorks2018.HumanResources.Employee
Select * into SQLShackDemo.HumanResources.EmployeeDepartmentHistory from
AdventureWorks2018.HumanResources.EmployeeDepartmentHistory
Select * into SQLShackDemo.HumanResources.EmployeePayHistory from
AdventureWorks2018.HumanResources.EmployeePayHistory
The columns in destination tables are created in the order specified in the select statement. All these columns have the exact name, data type, nullability property, and column value as in the source table.
目标表中的列是按select语句中指定的顺序创建的。 所有这些列都具有与源表中相同的确切名称,数据类型,可空性属性和列值。
If any of the tables contains an Identity column, the new column in the destination table will inherit the Identity property without the need to turn on the Identity_Insert.
如果任何表包含Identity列,则目标表中的新列将继承Identity属性,而无需打开Identity_Insert。
This is valid in most cases unless the Select statement contains Join, you are using Union to join multiple Select statements, the Identity column is mentioned many times in your select statement or the source of this Identity column is a remote data source.
在大多数情况下,这是有效的,除非Select语句包含Join,您正在使用Union来联接多个Select语句,在select语句中多次提到了Identity列,或者此Identity列的源是远程数据源。
If any one of these conditions mentioned is true, the column will be created with Not null property instead of inheriting the required Identity property.
如果上述任一条件为true,则将使用Not null属性创建该列,而不是继承所需的Identity属性。
To overcome this Identity issue, you can use the Identity SQL function in the select statement to create the Identity column.
要解决此Identity问题,可以在select语句中使用Identity SQL函数来创建Identity列。
Using SQL Server Export/Import wizard
使用SQL Server导出/导入向导
Another method that can be used to copy tables from the source database to the destination one is the SQL Server Export and Import wizard, which is available in SQL Server Management Studio. You have the choice to export from the source database or import from the destination one in order to transfer the data:
可用于将表从源数据库复制到目标数据库的另一种方法是SQL Server导出和导入向导,该向导可在SQL Server Management Studio中使用。 您可以选择从源数据库导出或从目标数据库导入,以便传输数据:
This method is a quick way to copy tables from the source database to the destination one if you arrange to copy the tables with no concern with the tables’ relationships and orders.
如果您安排复制表而不关心表的关系和顺序,则此方法是一种将表从源数据库复制到目标数据库的快速方法。
Using this method, the tables’ Indexes and keys will not be transferred. If you are interested in copying it, you need to generate scripts for these database objects.
使用此方法,将不会传输表的索引和键。 如果您有兴趣复制它,则需要为这些数据库对象生成脚本。
If these are Foreign keys connecting these tables together, you need to export the data in the correct order, otherwise the export wizard will fail.
如果这些是将这些表连接在一起的外键,则需要以正确的顺序导出数据,否则导出向导将失败。
Using Generate Scripts
使用生成脚本
SQL Server provides another way to generate script for the SQL Server databases with its objects and data. This script can be used to copy the tables’ schema and data from the source database to the destination one in our case.
SQL Server提供了另一种为SQL Server数据库及其对象和数据生成脚本的方法。 在本例中,此脚本可用于将表的架构和数据从源数据库复制到目标数据库。
SQL Scripting method is useful to generate one single script for the tables’ schema and data, including the indexes and keys. But again, this method doesn’t generate the tables’ creation script in the correct order if there are relations between the tables.
SQL脚本方法可用于为表的架构和数据(包括索引和键)生成一个脚本。 但是同样,如果表之间存在关系,则此方法不会以正确的顺序生成表的创建脚本。
Using the ApexSQL Diff and ApexSQL Data Diff combination
使用ApexSQL Diff和ApexSQL Data Diff组合
ApexSQL Diff is a useful SQL tool that can be used to find the differences between the databases from schema side and generate synchronization script to create these tables in the destination database in the correct order.
ApexSQL Diff是有用SQL工具,可用于从架构端查找数据库之间的差异,并生成同步脚本以按正确的顺序在目标数据库中创建这些表。
ApexSQL Data Diff also is another SQL tool that can be used to find the differences between the databases from data side and generate synchronization script to insert the data in the destination database tables, taking into consideration the Identity columns.
ApexSQL Data Diff还是另一个SQL工具,可用于从数据侧查找数据库之间的差异并生成同步脚本以将数据插入目标数据库表中,同时考虑了Identity列。
We can take benefits of these two tools together to generate the required scripts to create the tables to be copied with its objects, and copy the data from the source tables to the destination ones by following these steps:
我们可以利用这两个工具的优势来生成所需的脚本,以创建要与其对象一起复制的表,并按照以下步骤将数据从源表复制到目标表:
From the Data sources tab of the New project window, specify the source server name, the source database name and the Authentication method used to connect to the source server. You need to specify a valid user name and password if you choose SQL Server authentication.
在“ 新建项目”窗口的“ 数据源”选项卡中,指定源服务器名称,源数据库名称和用于连接到源服务器的身份验证方法。 如果选择SQL Server身份验证,则需要指定有效的用户名和密码。
Also specify the destination server name, the destination database name and the Authentication method used to connect to the destination server. You need to specify a valid user name and password if you choose SQL Server authentication.
还指定目标服务器名称,目标数据库名称和用于连接到目标服务器的身份验证方法。 如果选择SQL Server身份验证,则需要指定有效的用户名和密码。
You can change the role of each server specified as the source or destination one with a click on the Reverse button below data sources:
通过单击数据源下方的“ 反向”按钮,可以更改指定为源服务器或目标服务器的每台服务器的角色:
In our example, we are interested in the objects that exist in the AdventureWorks2018 database and not exist in the SQLShackDemo database, where we can find the tables to be copied.
在我们的示例中,我们对AdventureWorks2018数据库中存在的对象和SQLShackDemo数据库中不存在的对象感兴趣,在该数据库中可以找到要复制的表。
Now the script for creating the database tables with its objects are generated and in the correct order. You can simply run this script on the destination database to create the tables with a click on the Execute button:
现在,将以正确的顺序生成用于创建具有其对象的数据库表的脚本。 您只需在“ 执行”按钮上单击即可在目标数据库上运行此脚本以创建表:
From the Data sources tab of the New project window, specify the source server name, the source database name and the Authentication method used to connect to the source server. You need to specify a valid user name and password if you choose SQL Server authentication.
在“ 新建项目”窗口的“ 数据源”选项卡中,指定源服务器名称,源数据库名称和用于连接到源服务器的身份验证方法。 如果选择SQL Server身份验证,则需要指定有效的用户名和密码。
Also specify the destination server name, the destination database name and the Authentication method used to connect to the destination server. You need to specify a valid user name and password if you choose SQL Server authentication.
还指定目标服务器名称,目标数据库名称和用于连接到目标服务器的身份验证方法。 如果选择SQL Server身份验证,则需要指定有效的用户名和密码。
You can change the role of each server specified as the source or destination one with a click on the Reverse button:
您可以通过单击“ 反向”按钮来更改指定为源服务器或目标服务器的每个服务器的角色:
The main application window will be shown, that contains the data differences between the tables in the source and destination databases in addition to the identical and incomparable data.
将显示主应用程序窗口,其中包含相同和不可比较的数据以及源数据库和目标数据库中表之间的数据差异。
Here we are interested in the data differences between the tables in the source and destination databases, as in the previous steps we created these tables schema, but they are still empty.
在这里,我们对源数据库和目标数据库中的表之间的数据差异感兴趣,就像在前面的步骤中创建这些表模式一样,但它们仍然为空。
Now the script to insert the database tables, handling the Identity insert, are generated. You can simply run this script on the destination database with a click on the Execute button to insert the tables’ data:
现在,将生成用于处理身份标识插入的用于插入数据库表的脚本。 您只需单击“ 执行”按钮即可在目标数据库上运行此脚本,以插入表数据:
The combination of the ApexSQL Diff and ApexSQL Data Diff tools are useful to copy SQL database tables by generating complete scripts for these tables’ schema with indexes and keys and in the correct order, and the tables’ data handling the Identity column insert.
ApexSQL Diff和ApexSQL Data Diff工具的组合可用于复制SQL数据库表,方法是为这些表的模式生成带有索引和键的正确脚本,并按正确的顺序生成它们,并处理ID列插入的表数据。
Using ApexSQL Script
使用ApexSQL脚本
Another useful tool provided by ApexSQL that can be used to copy SQL Server tables data and schema from the source database to the destination one is the ApexSQL Script tool. This nice tool will create script for the database tables schema and data with the indexes and keys of these tables handling the Identity column insert.
ApexSQL提供的另一种有用的工具是ApexSQL脚本工具,可用于将SQL Server表数据和架构从源数据库复制到目标数据库。 这个好工具将为数据库表模式和数据创建脚本,并使用这些表的索引和键来处理Identity列插入。
Now a completed single script is generated. Just run it on the destination database and the tables will be copied completely. You can go through the script to check the tables’ creation scripts in the correct order as below:
现在,将生成完整的单个脚本。 只需在目标数据库上运行它,表将被完全复制。 您可以按照以下正确顺序浏览脚本以检查表的创建脚本:
Also, you can find the Insert statements for the tables’ data as follows:
另外,您可以找到表数据的Insert语句,如下所示:
ApexSQL Script is a very useful tool that can be used to copy SQL Server database tables from the source database to the destination one without any effort from your side to handle the tables’ creation order. Also, no action required from your side to handle the Identity insert.
ApexSQL Script是一种非常有用的工具,可用于将SQL Server数据库表从源数据库复制到目标数据库,而无需您方面的任何努力来处理表的创建顺序。 另外,您无需采取任何措施来处理身份证明插入。
Conclusion:
结论:
As you can see, multiple methods can be used to copy tables from the source database to the destination one, including schema and data. Most of these tools require a big effort from your side to copy the tables’ objects such as indexes and keys. Also, these tools will not handle the creation order for these tables if there are relations between it. From the data side, most of these tools need extra steps from your side to handle the Identity insert. Using the combination of the ApexSQL Diff and ApexSQL Data Diff or using ApexSQL Script, by a few numbers of simple steps, you will have a complete script for the database tables’ schema and data with no extra effort from your side to handle anything. Just run these scripts on the destination database and enjoy the result.
如您所见,可以使用多种方法将表从源数据库复制到目标数据库,包括模式和数据。 这些工具大多数都需要您付出很大的努力才能复制表的对象,例如索引和键。 此外,如果它们之间存在关系,这些工具将不处理这些表的创建顺序。 从数据方面来看,这些工具中的大多数都需要您采取额外的步骤来处理标识插入。 通过几个简单的步骤,使用ApexSQL Diff和ApexSQL Data Diff的组合,或者使用ApexSQL Script ,您将拥有数据库表的模式和数据的完整脚本,而无需您费力地处理任何事情。 只需在目标数据库上运行这些脚本即可享受结果。
翻译自: https://www.sqlshack.com/how-to-copy-tables-from-one-database-to-another-in-sql-server/