【SQLServer】使用T-SQL訪問遠程數據庫:openrowset 和 openquery 以及鏈接服務器的建立

█ 啓用/關閉Ad Hoc Distributed Queries
Ad Hoc Distributed Queries服務默認是關閉的,要使用openrowset 和 openquery訪問遠程數據庫,須要在本地啓用該服務sql

 

-- 啓用    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

 

█openrowset 和 openquery訪問遠程數據庫數據庫

一、openquery安全

【首先須要建立服務器鏈接】
方法1. SQLServerManagementStudio→服務器對象→鏈接服務器,右鍵(新建鏈接服務器),錄入服務器名,
          選擇服務器類型:SQLServer或其餘數據源
            (Oracle:Microsoft OLE DB Provide for Oracle)
            (SQL Server:Microsoft OLE DB Provide for SQL Server)
            (Sybace等:Microsoft OLE DB Provide for ODBC Drivers)
          安全性→使用此安全上下文創建鏈接:遠程數據庫用戶名及密碼
          服務器選項:若是要訪問遠程數據庫中的存儲過程,RPC/RPC OUT:ture服務器

方法2. 經過執行如下命令行實現:
          建立後在SQLServerManagementStudio→服務器對象→鏈接服務器看到新建立的鏈接服務器,RPC/RPC OUT須要手動修改ide

 

--建立連接服務器 
exec sp_addlinkedserver   '鏈接服務器名稱', '', 'SQLOLEDB', '服務器IP地址' 
exec sp_addlinkedsrvlogin '鏈接服務器名稱', 'false',null, '用戶名', '密碼' 

--刪除連接服務器 
exec sp_dropserver  '鏈接服務器名稱', 'droplogins' 

【查詢】函數

-- 無參數查詢(IPSERVER爲鏈接服務器名稱)
select * from openquery(IPSERVER,'select * from TB.dbo.employee')

-- 有參數查詢(IPSERVER爲鏈接服務器名稱)
declare @v_sql varchar(4000)
declare @v_employeeID varchar(5)
select @v_employeeID = '100001'
set @v_sql = 'select *  from openquery(IPSERVER,''select * from TB.dbo.employee where employeeID = ''''' + @v_employeeID + ''''''') as a'
exec (@v_sql)

【調用存儲過程及函數】spa

declare @v_btime datetime
declare @v_etime datetime
declare @v_sql varchar(4000)
select @v_btime = '2013.07.01'
select @v_etime = '2013.07.02'

-- 有參數調用函數(IPSERVER爲鏈接服務器名稱)
SET @v_sql = 'select * from openquery(IPSERVER, ''select * from TB.dbo.func_OrderDetail('''''+convert(varchar(19),@v_btime,120)+''''','''''+convert(varchar(19),@v_etime,120)+''''')'')'
exec (@v_sql)
        
-- 有參數調用存儲過程(IPSERVER爲鏈接服務器名稱)
SET @v_sql = 'select * from openquery(IPSERVER, ''exec TB.dbo.proc_OrderDetail '''''+convert(varchar(19),@v_btime,120)+''''','''''+convert(varchar(19),@v_etime,120)+''''''')'
exec (@v_sql)

 

二、openrowset命令行

【查詢】code

-- 無參數查詢(遠程SQLServer服務器)
select *  from openrowset('MSDASQL', 'DRIVER={SQL Server};SERVER=服務器IP地址;UID=用戶名;PWD=密碼;Initial Catalog=Pubs', 'select * from TB.dbo.employee') as a

select *  from openrowset('SQLOLEDB', 'DRIVER={SQL Server};SERVER=服務器IP地址;UID=用戶名;PWD=密碼;Initial Catalog=Pubs', 'select * from TB.dbo.employee') as a

select *  from openrowset('SQLOLEDB', 'SERVER=服務器IP地址;UID=用戶名;PWD=密碼;Initial Catalog=Pubs', 'select * from TB.dbo.employee') as a


-- 有參數查詢(遠程SQLServer服務器)
declare @v_sql varchar(4000)
declare @v_employeeID varchar(5)
select @v_employeeID = '10001'
set @v_sql = 'select *  from openrowset(''MSDASQL'', ''DRIVER={SQL Server};SERVER=服務器IP地址;UID=用戶名;PWD=密碼;Initial Catalog=Pubs'', ''select * from TB.dbo.employee where employeeID = ''''' + @v_employeeID + ''''''') as a'
exec (@v_sql)

declare @v_sql varchar(4000)
declare @v_employeeID varchar(5)
select @v_employeeID = '10001'
set @v_sql = 'select *  from openrowset(''SQLOLEDB'', ''DRIVER={SQL Server};SERVER=服務器IP地址;UID=用戶名;PWD=密碼;Initial Catalog=Pubs'', ''select * from TB.dbo.employee where employeeID = ''''' + @v_employeeID + ''''''') as a'
exec (@v_sql)

declare @v_sql varchar(4000)
declare @v_employeeID varchar(5)
select @v_employeeID = '10001'
set @v_sql = 'select *  from openrowset(''SQLOLEDB'', ''SERVER=服務器IP地址;UID=用戶名;PWD=密碼;Initial Catalog=Pubs'', ''select * from TB.dbo.employee where employeeID = ''''' + @v_employeeID + ''''''') as a'
exec (@v_sql)



-- 無參數查詢(遠程Oracle服務器)
select *  from openrowset('MSDAORA','實例名';'用戶名';'密碼','select * from TB.employee') as a

-- 有參數查詢(遠程Oracle服務器)
declare @v_sql varchar(4000)
declare @v_employeeID varchar(5)
select @v_employeeID = '10001'
set @v_sql = 'select *  from openrowset(''MSDAORA'', ''實例名'';''用戶名'';''密碼'', ''select * from TB.employee where employeeId = ''''' + @v_employeeID + ''''''') as a'
exec (@v_sql)

【調用存儲過程和函數】server

declare @v_btime datetime
declare @v_etime datetime
declare @v_sql varchar(4000)
select @v_btime = '2013.07.01'
select @v_etime = '2013.07.02'

-- 調用函數
set @v_sql = 'select * from openrowset(''MSDASQL'', ''DRIVER={SQL Server};SERVER=服務器IP地址;UID=用戶名;PWD=密碼;Initial Catalog=Pubs'', ''select * from TB.dbo.func_OrderDetail('''''+convert(varchar(19),@v_btime,120)+''''','''''+convert(varchar(19),@v_etime,120)+''''')'') as a'
exec (@v_sql)

-- 調用存儲過程
set @v_sql = 'select * from openrowset(''MSDASQL'', ''DRIVER={SQL Server};SERVER=服務器IP地址;UID=用戶名;PWD=密碼;Initial Catalog=Pubs'', ''exec TB.dbo.proc_OrderDetail '''''+convert(varchar(19),@v_btime,120)+''''','''''+convert(varchar(19),@v_etime,120)+''''''') as a'
exec (@v_sql)
相關文章
相關標籤/搜索