Sql 如何将一个数据库中的行插入到其他数据库中的同一个表中?
我试图将一个表中的一个特定行插入到具有相同名称但位于不同数据库中的其他表中,如下所示:Sql 如何将一个数据库中的行插入到其他数据库中的同一个表中?,sql,sql-server-2008,tsql,Sql,Sql Server 2008,Tsql,我试图将一个表中的一个特定行插入到具有相同名称但位于不同数据库中的其他表中,如下所示: Table Makers from database A id | name | type | comments 01 OSLO A None Table Makers from database B is empty but same structure as A id | name | type | comments Table Makers from data
Table Makers from database A
id | name | type | comments
01 OSLO A None
Table Makers from database B is empty but same structure as A
id | name | type | comments
Table Makers from database C is empty but same structure as A and B
id | name | type | comments
我有一个执行此任务的查询,但是我需要手动更改数据库的名称,也就是说
Use database_B
Go
Insert into Makers
([id], [name], [type], [comments])
select [id], [name], [type], [comments] from database_A
where id = '01'
Use database_C
Go
Insert into Makers
([id], [name], [type], [comments])
select [id], [name], [type], [comments] from database_A
where id = '01'
是否有其他方法可以在不重复相同插入代码的情况下执行相同的过程
欢迎提出任何改进此问题以使其更有价值的建议 如果两个数据库位于同一服务器中,则可以使用语法
[database].[schema].[table]
引用其他数据库中的表。这样就省去了use database_X
这句话,但仍然需要为每个目标表插入不同的内容。例如:
Insert into [database_C].[dbo].Makers ([id], [name], [type], [comments])
select [id], [name], [type], [comments]
from [database_A].[dbo].Makers
where id = '01'
Insert into [database_B].[dbo].Makers ([id], [name], [type], [comments])
select [id], [name], [type], [comments]
from [database_A].[dbo].Makers
where id = '01'
如果两个数据库位于同一服务器中,则可以使用语法
[database].[schema].[table]
引用其他数据库中的表。这样就省去了use database_X
这句话,但仍然需要为每个目标表插入不同的内容。例如:
Insert into [database_C].[dbo].Makers ([id], [name], [type], [comments])
select [id], [name], [type], [comments]
from [database_A].[dbo].Makers
where id = '01'
Insert into [database_B].[dbo].Makers ([id], [name], [type], [comments])
select [id], [name], [type], [comments]
from [database_A].[dbo].Makers
where id = '01'
如果有一个表是主表,其余都是副本,请考虑同义词。如果所有表都可以接受要传播的编辑,请考虑合并复制。如果实时对齐不是必需的,批量处理就足够了,考虑一个SSIS包,它有一个多播转换。
如果你有一个表是主表,其余的表只是拷贝,考虑同义词。如果所有表都可以接受要传播的编辑,请考虑合并复制。如果实时对齐不是必需的,并且批处理过程就足够了,那么考虑一个具有多个转换转换的SSIS包。