同一SQL之间数据复制:
1、当表目标表存在时:
insert into 目的数据库.表
select * from 源数据库.表
2、当目标表不存在时:
select *
into 目的数据库.表
from 源数据库.表
不同SQL之间数据复制:
如出现下列出错信息:
SQL Server 明止了对组件'Ad Hoc Distributed Queries'的 STATEMENTOpenRowset/OpenDatasource"的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_confiqure 启用Ad Hoc Distributed Queries。有关启用Ad Hoc Distributed Queries'的详细信息请参阅 SOL Server 联机丛书中的“外围应用配置器"。
解决方法
Ad Hoc Distributed Queries的启用与关闭
启用Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1 reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure
关闭Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',0 reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',0 reconfigure
insert into 数据库.表
select * from
openrowset ( ' SQLOLEDB ' , ' sql服务器名 ' ; ' 用户名 ' ; ' 密码 ' , 源数据库名 . dbo . 表名 )
where 条件
例:
insert into abc1
select * from
openrowset ( ' SQLOLEDB ' , ' SQL1 ' ; ' sa ' ; ' 123456 ' , [Test_20230509].dbo.abc1 )