SQL Server跨庫複製表數據錯誤的解決辦法

SQL Server跨庫複製表數據的解決辦法
 
跨庫複製表數據,有不少種方法,最多見的是寫程序來批量導入數據了,可是這種方法並非最優方法,今天就用到了一個很犀利的方法,能夠完美在 Sql Server 2005 和 Sql Server 2008 中執行!
 
格式以下:
insert into tableA
SELECT * FROM  www.2cto.com  
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.
  www.2cto.com  
翻譯:
 
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  www.2cto.com  
 
錯誤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.
 
這個真的很糾結,沒辦法,只有 google 了,以後發現能夠 在執行的 SQL 語句先後加上:SET IDENTITY_INSERT tableA ON
 
--執行的SQL
 
SET IDENTITY_INSERT tableB ON
 
試過以後,發現這個方法並不能解決,無奈,最後 找了半天,在國外 論壇找到了解決辦法,就是,要寫查入列的詳細信息
 
解決辦法:
insert into tableA (column1,column2.....)
SELECT * FROM  www.2cto.com  
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$])
 
跨庫複製表數據,一種很好的方法,呵呵,但願能對你們有所幫助!
相關文章
相關標籤/搜索