1

我正在编写一个可用于创建链接服务器的管理脚本。该脚本IF EXISTS在创建之前对链接服务器进行检查,这工作正常。

我已将sp_addlinkedsrvlogin详细信息添加到脚本中以生成访问链接服务器的适当权限。

当我再次尝试重新运行脚本时,它会引发错误:

消息 15190,级别 16,状态 1,过程 sp_dropserver,第 56 行服务器“LinkedServerName”仍有远程登录或链接登录。

所以我的问题是:

检查链接服务器是否存在登录的具体代码是什么?

这将允许我EXEC sp_droplinkedsrvlogin在发出条件之前有条件地EXEC sp_dropserver



现在的代码:

-- Set to master
    USE master
    GO

-- Set linked server vars
    DECLARE
        @ServerName         varchar(max)    =   'Server Name'       -- Name you wish to give to the Linked Server connection. To remove ambiguity you can name this the same as the Data Source you are connecting to.
        ,@ProductName       nvarchar(128)   =   'Product Name'      -- Type of OLE DB product you are connecting too, i.e. Oracle or SQL Server, e.g. SQl Server = N'MSSQL'.
        ,@ProviderName      nvarchar(128)   =   'Provider Name'     -- The OLE DB provider is expected to be registered with the specified PROGID in the registry, e.g. SQL Server = N'SQLNCLI'.
        ,@DataSource        nvarchar(4000)  =   'Source Name'       -- Is the name of the data source as interpreted by the OLE DB provider, i.e. the actual server name if connecting to MS SQL Server.
        ,@Location          nvarchar(4000)  =   'Location'          -- Not really worth using, listed for compelteness according to Reference 1 Location.           
        ,@ProviderString    nvarchar(4000)
        ,@CatalogName       nvarchar(4000)  =   'Database Name'     -- (Optional) Name of the database/catalog you will connect to. 
    SET
        @ProviderString =   'PROVIDER=SQLOLEDB; SERVER=' + @ServerName

-- Set linked server login vars
    DECLARE
        @Useself            varchar(8)      =   'Option'            -- 'TRUE' or 'FALSE' or NULL. Determines whether to connect to @rmtsrvname\@ServerName by impersonating local logins or explicitly submitting a login and password.
        ,@LocalLogin        varchar(100)    =   'Domain\Login'      -- Domain account.
        ,@User              varchar(50)     =   'SQL user'          -- Is the remote login used to connect to rmtsrvname when @useself\@Useself is FALSE.
        ,@Password          varchar(20)     =   'Password'          -- Is the password associated with @rmtuser\@User.

-- Drop linked server if already exists to prevent error when creating
    IF  EXISTS
        (
            SELECT  *
            FROM    sys.servers
            WHERE   server_id   !=  0
                    AND name    =   @ServerName
        )  
        EXEC    sp_dropserver
                @ServerName

-- Create linked server         
    EXEC    sp_addlinkedserver  
            @server         =   @ServerName
            ,@srvproduct    =   @ProductName
            ,@provider      =   @ProviderName
            ,@datasrc       =   @DataSource
            ,@location      =   @Location
            ,@provstr       =   @ProviderString
            ,@catalog       =   @CatalogName

-- Add permissions for usage            
    EXEC    sp_addlinkedsrvlogin 
            @rmtsrvname     =   @ServerName
            ,@useself       =   @Useself
            ,@locallogin    =   @LocalLogin
            ,@rmtuser       =   @User
            ,@rmtpassword   =   @Password

下面接受的答案。我还发现,您可以在删除链接服务器时通过将参数传递,@droplogins = 'droplogins'EXEC sp_dropserverproc 来强制删除登录:

IF  EXISTS
    (
        SELECT  1
        FROM    sys.servers
        WHERE   server_id   !=  0
                AND name    =   @ServerName
    )  
    EXEC    sp_dropserver
            @server         =   @ServerName
            ,@droplogins    =   'droplogins'
4

1 回答 1

2
select 1 
from sys.remote_logins l
inner join sys.servers s
on l.server_id = s.server_id
where s.name = @ServerName 
and l.remote_name = @User --check on remote user

或者

select 1 
from sys.remote_logins l
inner join sys.servers s
    on l.server_id = s.server_id
inner join sys.server_principals p
    on l.local_principal_id = p.principal_id
where s.name = ServerName 
and p.name = @LocalLogin --check on local user
于 2012-10-25T01:27:02.633 回答