跨伺服器訪問SQLSERVER

來源:互聯網
上載者:User

一,  設定sp_configure

exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

 執行sql :

SELECT * FROM OPENROWSET ( 'SQLOLEDB ', 'WIN-P8AKPFGRVGZ'; 'sa'; '6YHN7ujm',ReportServer.dbo.batch)

關閉 把相應設定為0即可。

二,sp_addlinkedserver

--建立linkServer
 EXEC SP_ADDLINKEDSERVER  'srv_lnk','','SQLOLEDB','WIN-P8AKPFGRVGZ'
--登陸linkServer
 EXEC SP_ADDLINKEDSRVLOGIN  'srv_lnk','false',null,'sa','6YHN7ujm'
--查詢linkServer的資料庫DataBaseA的表TableA
 SELECT * FROM srv_lnk.ReportServer.dbo.batch
 --List the tables in the linked server
 EXEC sp_tables_ex srv_lnk

關閉  EXEC SP_DROPSERVER    'srv_lnk', 'droplogins'

 

--更新本地表

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   'WQJK', ' ', '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 本地表

注意 遠程一定需要開防火牆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.