sqlserver跨數據庫查詢數據


--一、操作提權
/*
--啟用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
*/

declare @A varchar(100)=null;
declare @B varchar(100)=null;

--查詢本地庫數據信息
select top 1 @A =sum(NowNum)  from table1

--啟動遠程服務器連接,添加一次就可以
--EXEC sp_addlinkedserver '服務器地址',N'SQL Server'        --將遠端數據庫映射到本地數據
--EXEC sp_addlinkedsrvlogin '服務器地址', 'false', NULL, '數據庫賬號', '密碼'  -- 啟動鏈接遠端服務器 

--二、跨數據庫查詢數據
select top 1  @B= sum(NowNum) from [服務器地址].數據庫名.dbo.[表名]

 


免責聲明!

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



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