sql sever跨數據庫復制數據的方法


1,用Opendatasource系統函數

詳細的用法已經注釋在sql代碼中了。這個是在sqlserver到sqlserver之間的倒數據。2005,2008,2012應該都是適用的。

--從遠程服務器192.168.66.154上查詢100條數據,然后導入到dbo.dquestiondata中
insert into dbo.dquestiondata
select top 100 * from
opendatasource('sqloledb','data source=192.168.6.154;user id=sa;password=xxxxxx').Answer.dbo.DQuestionData
--opendatasource 是一個系統函數,第一個參數是Provider Name,第二個參數是Oledb鏈接字符串,
--注意連接字符串里沒有指定數據庫;數據庫名稱,Schema,表名在opendatasource函數后面書寫。

--執行上面的語句會報如下的錯,原因是沒有開啟遠程查詢支持
    --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.

--解決辦法1:在圖形界面中,SQL Server 安全配置-->機能的安全配置-->開啟遠程查詢Openrowset和opendatasource的支持

--解決辦法2:用sp_confing系統存儲過程,以命令行的方式開啟
--首先必須開啟‘show advanced options’高級選項,因為‘Ad Hoc Distributed Queries’屬於高級選項
exec sp_configure 'show advanced options' ,1
reconfigure
--下面這步可以省略,不帶參數的sp_configure,表示查看所有可用的選項
--記住上面一定要加reconfigure語句,目的是使選項立即生效,不然我們
--還是看不到'Ad Hoc Distributed Queries'高級選項
exec sp_configure
--打開'Ad Hoc Distributed Queries'選項
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

--記得用完選項后,關閉這些選項
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
--查看一下是不是關閉了
exec sp_configure

 2,用openrowset系統函數

--1,Microsoft OLE DB Provider for SQL Server
--注意第二部分鏈接字符串的寫法很是奇怪Server,user,passwrod是用“;”連接的。
select top 100 * from
openrowset('sqloledb','192.168.6.154';'sa';'xxxxx',Answer.dbo.DQuestionData) 

--2,ODBC數據源的方式:Microsoft OLE DB Provider for ODBC Drivers
select  top 100 * from
openrowset('MSDASQL','DRIVER={SQL Server};SERVER=192.168.6.154;UID=sa;PWD=xxxxx',Answer.dbo.DQuestionData) 

 上面的兩種方法都會用到oledb提供商的名稱,下面的這個系統存儲過程可以查看oledb提供程序的名稱

--用於查看oledb提供者名稱
EXEC master..xp_enum_oledb_providers 

3,用鏈接服務器

如果要多次用到遠程查詢,每次都寫那么長的鏈接字符串有點麻煩。可以考慮重復利用。

用鏈接服務器可以很好的解決這個問題

-- 創建鏈接服務器
exec sp_addlinkedserver 'svr_link','','sqloledb','192.168.6.154'
-- 創建登錄信息
exec sp_addlinkedsrvlogin 'svr_link','false',null,'sa','xxxx'

--查詢 格式為:鏈接服務器.數據庫名.架構名.表名
select top 100 * from svr_link.Answer.dbo.DQuestionData

-- 刪除鏈接服務器
exec sp_dropserver 'svr_link','droplogins'

需要注意的幾點:

1,雖然上面的例子都是從遠程服務器select數據到本地,

但我們同樣可以將本地的數據導向遠程服務器

-- 往遠程數據庫192.168.6.154的Answer數據庫的test表插入兩條數據
insert into svr_link.Answer.dbo.test
select 1,'zhang' union all
select 2,'wang'

2,有了鏈接服務器,就可以用openquery系統函數了

--檢索數據
select * from 
openquery(svr_link,'select  * from Answer.dbo.test')
-- 插入數據
insert into openquery(svr_link,'select  * from Answer.dbo.test')
select 3,'li'

3,opendatasource和鏈接服務器都是只返回Server部分,要查詢表需要進一步指定數據庫,架構和表名。而openrowset和openquery返回的是記錄集。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM