SQL2008将服务器的数据库表数据插入到本地数据库

一,配置参数

exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
RECONFIGURE

若不配置参数会出现,提示这个错误:

SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。有关启用 'Ad Hoc Distributed Queries' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"。

二,在本地写数据库语句

insert into 表名(列名) SELECT * FROM OPENDATASOURCE('SQLOLEDB', 'Data Source=120.0.0.1;User ID=sa;Password=123456').服务器数据库名.表名

这里要注意自己表的字段中是否有标示,有标示会出现:

当 IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值

PS:以上列名需跟数据库表的列名一致

三,还原配置

exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
原文地址:https://www.cnblogs.com/May-day/p/5888048.html