在SQL Server中的数据库之间复制行

时间:2022-12-22 09:07:06

What's the most efficient way to copy a large amount of rows from a table in one database to another table in a different database that has the exact same structure?

将大量行从一个数据库中的表复制到另一个具有完全相同结构的数据库中的表的最有效方法是什么?

7 个解决方案

#1


13  

If your log IO subsystem allows it, then:

如果您的日志IO子系统允许它,那么:

INSERT INTO target(field1, field2, field3)
SELECT field1, field2, field3 FROM source;

But having the entire transfer occur in one single statement means one single transaction, which means that x2.5 data size log has to be generated and kept during the entire statement. Ie. if you transfer 50Gb, your target db log will grow to 250Gb (even if the recovery mode is set to simple!).

但是,在一个语句中进行整个传输意味着一个事务,这意味着必须在整个语句中生成并保留x2.5数据大小日志。 IE浏览器。如果传输50Gb,目标数据库日志将增长到250Gb(即使恢复模式设置为简单!)。

If you are concerned about log, then you have to transfer in batches. You can still do the INSERT ... SELECT trick, but your SELECT has to use some key range and batch acceptable number of rows.

如果您担心日志,那么您必须批量转移。您仍然可以执行INSERT ... SELECT技巧,但您的SELECT必须使用一些键范围和批次可接受的行数。

Ultimately, you can always do a bcp out followed by a bcp in, it will work pretty fast and is probably the fastest way to get a reliable transfer going.

最终,您可以随时执行bcp out,然后执行bcp in,它将非常快速地工作,并且可能是获得可靠传输的最快方式。

#2


15  

If the databasesare on the same server, then it's trivial - you'd do it as if you were copying between tables in the same database, i.e.:

如果数据库位于同一台服务器上,那么它就是微不足道的 - 你就像在同一数据库中的表之间复制一样,即:

INSERT INTO targetdatabase..targettable (col1, col2, col3, col4)
    SELECT col1, col2, col3, col4 FROM sourcedatabase..sourcetable

If the databases are on different servers, you'll need to look at using one of OPENQUERY, OPENROWSET or linked servers to do the query, but fundamentally even with all these, you'd still be writing a variation on the command above.

如果数据库位于不同的服务器上,您需要使用OPENQUERY,OPENROWSET或链接服务器之一来进行查询,但从根本上说,即使是所有这些,您仍然会在上面的命令中编写变体。

Alternatively, it's a case of BCP out and BCP in or using SQL Server Management Studio's data transfer wizard.

或者,这是一个BCP输出和BCP或使用SQL Server Management Studio的数据传输向导的情况。

#3


3  

A simple way is to open SSMS and Right click on database and go to Tasks > Import Data and follow the wizard to set source and destination.

一种简单的方法是打开SSMS并右键单击数据库,然后转到任务>导入数据,然后按照向导设置源和目标。

This way if data exists on different systems and even if you wish to change structure you can do.

这样,如果数据存在于不同的系统上,即使您希望更改结构,也可以这样做。

Also append, or cleanout data at same time from destination.

还可以从目的地同时附加或清除数据。

#4


0  

I would consider a bulk insert:

我会考虑批量插入:

http://msdn.microsoft.com/en-us/library/ms188365.aspx

http://msdn.microsoft.com/en-us/library/ms188365.aspx

#5


0  

you can simply user Copy Database Wizard
http://www.packtpub.com/article/copying-database-sql-2008-copy-database-wizard

你可以简单地用户复制数据库向导http://www.packtpub.com/article/copying-database-sql-2008-copy-database-wizard

#6


0  

insert into CRS_New.dbo.SalesUpdateRBCustomerExternalRelationShipData_V1 (RBCustomerExternalEdgeKeyFather,RBCustomerExternalEdgeKeySon,EdgeLevelIndicator,Status,Edition,StatusValidFrom,ActiveIndicator,AddAttributeList_id) select RBCustomerExternalEdgeKeyFather,RBCustomerExternalEdgeKeySon,EdgeLevelIndicator,Status,Edition,StatusValidFrom,ActiveIndicator,AddAttributeList_id from CRS.dbo.SalesUpdateRBCustomerExternalRelationShipData_V11

插入CRS_New.dbo.SalesUpdateRBCustomerExternalRelationShipData_V1(RBCustomerExternalEdgeKeyFather,RBCustomerExternalEdgeKeySon,EdgeLevelIndicator,状态,版,StatusValidFrom,ActiveIndicator,AddAttributeList_id)选择RBCustomerExternalEdgeKeyFather,RBCustomerExternalEdgeKeySon,EdgeLevelIndicator,状态,版,StatusValidFrom,ActiveIndicator,AddAttributeList_id从CRS.dbo.SalesUpdateRBCustomerExternalRelationShipData_V11

#7


0  

Insert into targetdatabase.dbo.tablename
select * from sourcedatabase.dbo.tablename

NOTE: .dbo is important

注意:.dbo很重要

If the columns are different in source and target tables then explicitly mention the columns for both tables. Do keep in mind that data types should be same

如果源表和目标表中的列不同,则显式提及两个表的列。请记住,数据类型应该相同

#1


13  

If your log IO subsystem allows it, then:

如果您的日志IO子系统允许它,那么:

INSERT INTO target(field1, field2, field3)
SELECT field1, field2, field3 FROM source;

But having the entire transfer occur in one single statement means one single transaction, which means that x2.5 data size log has to be generated and kept during the entire statement. Ie. if you transfer 50Gb, your target db log will grow to 250Gb (even if the recovery mode is set to simple!).

但是,在一个语句中进行整个传输意味着一个事务,这意味着必须在整个语句中生成并保留x2.5数据大小日志。 IE浏览器。如果传输50Gb,目标数据库日志将增长到250Gb(即使恢复模式设置为简单!)。

If you are concerned about log, then you have to transfer in batches. You can still do the INSERT ... SELECT trick, but your SELECT has to use some key range and batch acceptable number of rows.

如果您担心日志,那么您必须批量转移。您仍然可以执行INSERT ... SELECT技巧,但您的SELECT必须使用一些键范围和批次可接受的行数。

Ultimately, you can always do a bcp out followed by a bcp in, it will work pretty fast and is probably the fastest way to get a reliable transfer going.

最终,您可以随时执行bcp out,然后执行bcp in,它将非常快速地工作,并且可能是获得可靠传输的最快方式。

#2


15  

If the databasesare on the same server, then it's trivial - you'd do it as if you were copying between tables in the same database, i.e.:

如果数据库位于同一台服务器上,那么它就是微不足道的 - 你就像在同一数据库中的表之间复制一样,即:

INSERT INTO targetdatabase..targettable (col1, col2, col3, col4)
    SELECT col1, col2, col3, col4 FROM sourcedatabase..sourcetable

If the databases are on different servers, you'll need to look at using one of OPENQUERY, OPENROWSET or linked servers to do the query, but fundamentally even with all these, you'd still be writing a variation on the command above.

如果数据库位于不同的服务器上,您需要使用OPENQUERY,OPENROWSET或链接服务器之一来进行查询,但从根本上说,即使是所有这些,您仍然会在上面的命令中编写变体。

Alternatively, it's a case of BCP out and BCP in or using SQL Server Management Studio's data transfer wizard.

或者,这是一个BCP输出和BCP或使用SQL Server Management Studio的数据传输向导的情况。

#3


3  

A simple way is to open SSMS and Right click on database and go to Tasks > Import Data and follow the wizard to set source and destination.

一种简单的方法是打开SSMS并右键单击数据库,然后转到任务>导入数据,然后按照向导设置源和目标。

This way if data exists on different systems and even if you wish to change structure you can do.

这样,如果数据存在于不同的系统上,即使您希望更改结构,也可以这样做。

Also append, or cleanout data at same time from destination.

还可以从目的地同时附加或清除数据。

#4


0  

I would consider a bulk insert:

我会考虑批量插入:

http://msdn.microsoft.com/en-us/library/ms188365.aspx

http://msdn.microsoft.com/en-us/library/ms188365.aspx

#5


0  

you can simply user Copy Database Wizard
http://www.packtpub.com/article/copying-database-sql-2008-copy-database-wizard

你可以简单地用户复制数据库向导http://www.packtpub.com/article/copying-database-sql-2008-copy-database-wizard

#6


0  

insert into CRS_New.dbo.SalesUpdateRBCustomerExternalRelationShipData_V1 (RBCustomerExternalEdgeKeyFather,RBCustomerExternalEdgeKeySon,EdgeLevelIndicator,Status,Edition,StatusValidFrom,ActiveIndicator,AddAttributeList_id) select RBCustomerExternalEdgeKeyFather,RBCustomerExternalEdgeKeySon,EdgeLevelIndicator,Status,Edition,StatusValidFrom,ActiveIndicator,AddAttributeList_id from CRS.dbo.SalesUpdateRBCustomerExternalRelationShipData_V11

插入CRS_New.dbo.SalesUpdateRBCustomerExternalRelationShipData_V1(RBCustomerExternalEdgeKeyFather,RBCustomerExternalEdgeKeySon,EdgeLevelIndicator,状态,版,StatusValidFrom,ActiveIndicator,AddAttributeList_id)选择RBCustomerExternalEdgeKeyFather,RBCustomerExternalEdgeKeySon,EdgeLevelIndicator,状态,版,StatusValidFrom,ActiveIndicator,AddAttributeList_id从CRS.dbo.SalesUpdateRBCustomerExternalRelationShipData_V11

#7


0  

Insert into targetdatabase.dbo.tablename
select * from sourcedatabase.dbo.tablename

NOTE: .dbo is important

注意:.dbo很重要

If the columns are different in source and target tables then explicitly mention the columns for both tables. Do keep in mind that data types should be same

如果源表和目标表中的列不同,则显式提及两个表的列。请记住,数据类型应该相同