SQL Server跨庫查詢

2022-06-10 03:12:10 字數 2825 閱讀 3988

方式一:

語句select * from 資料庫a.dbo.表a a, 資料庫b.dbo.表b b where a.field=b.field

"dbo"可以省略 如

select * from 資料庫a..表a a, 資料庫b..表b b where a.field=b.field

方式二(在乙個資料庫掛上另乙個資料庫的外鏈):

sqlserver資料庫:

--這句是對映乙個遠端資料庫

exec sp_addlinkedserver '遠端資料庫的ip或主機名',n'sql server'

--這句是登入遠端資料庫

exec sp_addlinkedsrvlogin '遠端資料庫的ip或主機名', 'false', null, '登入名', '密碼'

--登入後,可以用以下格式操作遠端資料庫中的物件

select * from [遠端資料庫的ip或主機名].[資料庫名].[dbo].[表名]

insert into openrowset('sqloledb','192.168.0.100';'sa';'10060','select * from knss2009.dbo.yw_kck') select * from yw_kck

示例:--建立鏈結伺服器 

exec sp_addlinkedserver   'itsv ', ' ', 'sqloledb ', '遠端伺服器名或ip位址 ' 

exec sp_addlinkedsrvlogin 'itsv ', 'false ',null, '使用者名稱 ', '密碼 ' 

--查詢示例 

select * from itsv.資料庫名.dbo.表名 

--匯入示例 

select * into 表 from itsv.資料庫名.dbo.表名 

--以後不再使用時刪除鏈結伺服器 

exec sp_dropserver  'itsv ', 'droplogins ' 

方式三:

--連線遠端/區域網資料(openrowset/openquery/opendatasource) 

--1、openrowset (比較推薦這種做法)

--查詢示例 

select * from openrowset( 'sqloledb ', 'sql伺服器名 '; '使用者名稱 '; '密碼 ',資料庫名.dbo.表名) 

--生成本地表 

select * into 表 from openrowset( 'sqloledb ', 'sql伺服器名 '; '使用者名稱 '; '密碼 ',資料庫名.dbo.表名) 

--把本地表匯入遠端表 

insert openrowset( 'sqloledb ', 'sql伺服器名 '; '使用者名稱 '; '密碼 ',資料庫名.dbo.表名) 

select *from 本地表 

--更新本地表 

update b 

set b.列a=a.列a 

from openrowset( 'sqloledb ', 'sql伺服器名 '; '使用者名稱 '; '密碼 ',資料庫名.dbo.表名)as a inner join 本地表 b 

on a.column1=b.column1 

--openquery用法需要建立乙個連線 

--首先建立乙個連線建立鏈結伺服器 

exec sp_addlinkedserver   'itsv ', ' ', 'sqloledb ', '遠端伺服器名或ip位址 ' 

--查詢 

select * 

from openquery(itsv,  'select *  from 資料庫.dbo.表名 ') 

--把本地表匯入遠端表 

insert openquery(itsv,  'select *  from 資料庫.dbo.表名 ') 

select * from 本地表 

--更新本地表 

update b 

set b.列b=a.列b 

from openquery(itsv,  'select * from 資料庫.dbo.表名 ') as a  

inner join 本地表 b on a.列a=b.列a 

--3、opendatasource/openrowset 

select   * 

from   opendatasource( 'sqloledb ',  'data source=ip/servername;user id=登陸名;password=密碼 ').test.dbo.roy_ta 

--把本地表匯入遠端表 

insert opendatasource( 'sqloledb ',  'data source=ip/servername;user id=登陸名;password=密碼 ').資料庫.dbo.表名 

select * from

跨庫取數使用示例(注意做判斷):

if exists(select 1 from sys.synonyms where name='syn305_ys_costandcashset')

drop synonym syn305_ys_costandcashset

gocreate synonym syn305_ys_costandcashset for [dotnet_erp305_hnjy].dbo.ys_costandcashset

go不允許遠端訪問出現異常解決方案:

exec sp_configure 'show advanced options',1

reconfigure

exec sp_configure 'ad hoc distributed queries',1

reconfigure

SQLServer跨庫查詢

用openrowset連線遠端sql或插入資料 如果只是臨時訪問,可以直接用openrowset 查詢示例 select from openrowset sqloledb sql伺服器名 使用者名稱 密碼 資料庫名.dbo.表名 在跨庫查詢時需要啟用ad hoc distributed querie...

sql server跨庫查詢

方式一 dbo可以省略 select from 資料庫a.dbo.表a a,資料庫b.dbo.表b b where a.field b.field 方式二 在乙個資料庫掛上另乙個資料庫的外鏈 對映乙個遠端資料庫 exec sp addlinkedserver 遠端資料庫的ip或主機名 n sql s...

sqlserver 跨庫查詢

select from opendatasource sqloledb data source 遠端ip user id sa password 密碼 庫名.dbo.表名 where 條件 解除sql阻止 sql server 阻止了對元件 adhocdistributedqueries 的 sta...