SQL Server 創(chuàng)建連接服務(wù)器 link server 存儲過程
發(fā)表時間:2023-07-27 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]軟件等級:更新時間:2016-11-11版本號:v5.7.10 MySQL Server x64官方正式版免費下載立即下載 SQL Server 建立連接服務(wù)器 link server ...
SQL Server 建立連接服務(wù)器 link server 存儲過程
有時候我們需要查詢遠(yuǎn)程服務(wù)器的數(shù)據(jù)庫,查詢的方式 [servername].[dbname].[owner].[object]
比如 Select * from [172.10.10.2].[test].dbo.table
但是在沒有建立link server之前是不可以查的,這里我寫了一個通用的存儲過程來建立link server的。
代碼如下:
Create procedure [dbo].[usp_PUB_Addlinkserver]
(
@ServerIP varchar(30),
@UserID varchar(50),
@Password varchar(50)
)
AS
Declare @Testlinkserver varchar(500)
IF Exists(select * from master.dbo.sysservers where srvname=@ServerIP)
BEGIN
Select 'Failed' as iResult,'Link Server already exist!' as iDescription,@ServerIP as ServerIP
--Exec sp_dropserver @ServerIP,'droplogins'
return
END
Begin Try
Exec sp_addlinkedserver @ServerIP,'','SQLOLEDB',@ServerIP
Exec sp_addlinkedsrvlogin @ServerIP,'false',null,@UserID,@Password
set @Testlinkserver='Select top 1 *into #tmp from ['+@ServerIP+'].master.dbo.sysservers '
Exec(@Testlinkserver)
Insert into __Temp
Select 'Succeed' as iResult,'Operation Completed!' as iDescription,@ServerIP as ServerIP
return
END Try
BEGIN Catch
Insert into __Temp
Select 'Failed' as iResult,'UID password incorrect!' as iDescription,@ServerIP as ServerIP
Exec sp_dropserver @ServerIP,'droplogins'
END Catch
學(xué)習(xí)教程快速掌握從入門到精通的電腦知識