經過存儲過程(SP)實現SQL Server連接服務器(LinkServer)的添加

1. 背景

當系統的微服務化作的不是很高的時候,部分功能要經過DB LinkServer 來實現跨 Server 查詢,固然,有時候BI抽數據、DBA數據庫維護可能也會建立LinkServer。sql

特別是當 DB遷移的時候,咱們須要檢查、建立 DB LinkServer。數據庫

 

2.腳本實現

下面是咱們建立的一個存儲過程,經過這個存儲過程來實現簡單、快速的添加連接服務器。服務器

 

USE [DBA_Manager]
GO

/****** Object:  StoredProcedure [dbo].[USP_Create_DBLink]    Script Date: 2019/7/5 13:52:50 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:        <Author,,Carson>
-- Create date: <Create Date,2018-06-18,>
-- Description:    <Description,實現建立DB LinkServer的腳本化,>
-- =============================================
CREATE PROCEDURE [dbo].[USP_Create_DBLink] 
    -- Add the parameters for the stored procedure here
    @ServerIP varchar(20),@SQLUserName varchar(20)='',@PassW varchar(20)='',@DelCurLinks varchar(10)='N', @Result nvarchar(3000)='' output
AS
BEGIN

SET NOCOUNT ON;

declare @OriginalSQL nvarchar(3000)
declare @sSQL nvarchar(3000)
 
----------------------------------------------

---判斷指定的ServerIP是否已存在DBLinkServer,結合@DelCurLinks輸入參數判斷是否刪除重建

if @DelCurLinks='Y' and exists(select srvname   from master.dbo.sysservers where srvname <> SERVERPROPERTY('servername') and srvname=@ServerIP)
begin
    set @sSQL=' EXEC master.dbo.sp_dropserver @server=N'''+@ServerIP+''', @droplogins=''droplogins''' 
    Print @sSQL    
    exec sp_executesql @sSQL 
end

if not exists(select srvname   from master.dbo.sysservers where srvname <> SERVERPROPERTY('servername') and srvname=@ServerIP) or @DelCurLinks='Y'
begin
        --create  script 
        set @OriginalSQL='/****** Object:  LinkedServer [<ServerIP>] ******/
                EXEC master.dbo.sp_addlinkedserver @server = N''<ServerIP>'', @srvproduct=N''SQL Server''
                 /* For security reasons the linked server remote logins password is changed with ######## */
                EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N''<ServerIP>'',@useself=N''False'',@locallogin=NULL,@rmtuser=N''<UserName>'',@rmtpassword=''<PWD>''
                --GO
                EXEC master.dbo.sp_serveroption @server=N''<ServerIP>'', @optname=N''collation compatible'', @optvalue=N''true''
                --GO
                EXEC master.dbo.sp_serveroption @server=N''<ServerIP>'', @optname=N''data access'', @optvalue=N''true''
                --GO
                EXEC master.dbo.sp_serveroption @server=N''<ServerIP>'', @optname=N''rpc'', @optvalue=N''true''
                --GO
                EXEC master.dbo.sp_serveroption @server=N''<ServerIP>'', @optname=N''rpc out'', @optvalue=N''true''
                --GO
                EXEC master.dbo.sp_serveroption @server=N''<ServerIP>'', @optname=N''use remote collation'', @optvalue=N''true''
                --GO
                EXEC master.dbo.sp_serveroption @server=N''<ServerIP>'', @optname=N''remote proc transaction promotion'', @optvalue=N''true''
                --GO
                '
        
        set  @sSQL=REPLACE( @OriginalSQL, '<ServerIP>',@ServerIP )
        set  @sSQL=REPLACE( @sSQL, '<UserName>',@SQLUserName )
        set  @sSQL=REPLACE( @sSQL, '<PWD>',@PassW )

        begin try
             exec sp_executesql @sSQL 
             print @sSQL
             print 'Create Link Server['+ @ServerIP +'] successfully!'
        end try

        begin catch
            print 'Create Link Server ['+ @ServerIP +'] fail! ErrMsg: '+ERROR_MESSAGE()     
        end catch

end 

if @DelCurLinks='N' and exists(select srvname   from master.dbo.sysservers where srvname <> SERVERPROPERTY('servername') and srvname=@ServerIP)
begin
    print 'Create Link Server ['+ @ServerIP +'] fail! ErrMsg: Find DBLinkserver of the Same Name ,Please Check it.'
end

END


GO

 

3.方法使用

方法 1: 只輸入IP、UID、PWD三個參數,@DelCurLinks不顯示輸入【此時,@DelCurLinks默認爲 N,指明當存在相同的LinkServer時,不刪除直接退出。】微服務

Exec USP_Create_DBLink '172.XXX.XXX.XXX','UID','PWD'

方法 2:輸入IP、UID、PWD、DelCurLinks 四個參數,顯示指明當存在相同的LinkServer時,不刪除直接退出。spa

Exec USP_Create_DBLink '172.XXX.XXX.XXX','UID','PWD','N'

方法 3:輸入IP、UID、PWD、DelCurLinks 四個參數,顯示指明當存在相同的LinkServer時,刪除從新建立code

Exec USP_Create_DBLink '172.XXX.XXX.XXX','UID','PWD','Y'

 

4. 其它知識

 (1) 查詢本SQL Server 已建立的全部實例server

select srvname as '連接服務器'   from master.dbo.sysservers where srvname <> SERVERPROPERTY('servername')

(2)刪除已創建的連接服務器(LinkServer)blog

EXEC master.dbo.sp_dropserver @server=N'172.XXX.XXX.XXX', @droplogins='droplogins'
相關文章
相關標籤/搜索