若2个数据库在同一台机器上:
insert into DataBase_A..Table1(col1,col2,col3----)
select col11,col22,col33-- from DataBase_B..Table11
若不在同一台机器上,则是本文要讲的内容:
-----------------------------------------------------------------------------------------
INSERT INTO [目标数据库名].[dbo].[表名]
([Target_ID]
,[Target_Name])
select * from
OPENDATASOURCE(‘SQLOLEDB‘,‘Data Source=服务器;DBN=源数据库名;UID=用户名;PWD=密码‘).源数据库名.dbo.[表名]
但上述查询需要开启外围数据库查询
先选中 master 数据库,新建一个查询,执行以下的sql
开户外围:
exec sp_configure ‘show advanced options‘,1
reconfigure
exec sp_configure ‘Ad Hoc Distributed Queries‘,1
reconfigure
-------------------------------
关闭外围:
exec sp_configure ‘show advanced options‘,0
reconfigure
exec sp_configure ‘Ad Hoc Distributed Queries‘,0
reconfigure
时间: 2024-10-12 10:45:20