SQL Server 跨库复制表数据的解决办法

跨库复制表数据,有很多种方法,最常见的是写程序来批量导入数据了,但是这种方法并不是最优方法,今天就用到了一个很犀利的方法,可以完美在 Sql Server 2005 和 Sql Server 2008 中执行!

格式如下:

insert into tableA 
SELECT * FROM         
OPENDATASOURCE('SQLOLEDB', 'Data Source=127.0.0.1;User ID=sa;Password=sasasa').databaseName.dbo.tableB 

找到这个方法后,准备执行,可是却并不太顺利,跨库复制表数据的途中,接连出现两个错误,第一个错误:

SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.

翻译:

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

解决办法:

启用  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 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure

错误2 :

An explicit value for the identity column in table 'cms_TagSubject' can only be specified when a column list is used and IDENTITY_INSERT is ON.

最后 找了半天,在国外论坛找到了解决办法,就是,要写查入列的详细信息

解决办法:

insertinto tableA (column1,column2.....) 
SELECT*FROM
OPENDATASOURCE('SQLOLEDB''Data Source=127.0.0.1,3422;User ID=sa;Password=sasasa;').databaseName.dbo.tableB 

终于大功告成,另外,利用这种方法,还是可以直接从 Excel 里面查询的,呵呵,真强大:

SELECT *  FROM OPENROWSET( 'MICROSOFT.JET.OLEDB.4.0','Excel 8.0;IMEX=1;HDR=YES;DATABASE=D:\a.xls',[sheet1$])

跨库复制表数据,一种很好的方法。

转载自:http://www.cnblogs.com/xunziji/archive/2011/04/11/2012644.html

作者:沐雪
文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者和博客园共有,如需转载恳请注明。
如果您觉得阅读这篇博客让你有所收获,请点击右下方【推荐】
找一找教程网-随时随地学软件编程 http://www.zyiz.net/

原文地址:https://www.cnblogs.com/puzi0315/p/2644841.html