Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8742011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:25:29+00:00 2026-06-13T11:25:29+00:00

I’m writing an admin script I can use for creating linked servers. The script

  • 0

I’m writing an admin script I can use for creating linked servers. The script does a IF EXISTS check on the linked server before creating, this works fine.

I have added the sp_addlinkedsrvlogin details to the script to generate the appropriate permissions to access the linked server.

When I try to re-run thescript again it throws the error:

Msg 15190, Level 16, State 1, Procedure sp_dropserver, Line 56 There
are still remote logins or linked logins for the server
‘LinkedServerName’.

So my question is:

What is the specific code to check for existence of a login for a linked server?

This will allow me to conditionally EXEC sp_droplinkedsrvlogin before issuing the conditional EXEC sp_dropserver


Code as it stands now:

-- 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

Answer accepted below. I also found that you can force logins to be dropped when dropping a linked server by passing the argument ,@droplogins = 'droplogins' to the EXEC sp_dropserver proc:

IF  EXISTS
    (
        SELECT  1
        FROM    sys.servers
        WHERE   server_id   !=  0
                AND name    =   @ServerName
    )  
    EXEC    sp_dropserver
            @server         =   @ServerName
            ,@droplogins    =   'droplogins'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:25:30+00:00Added an answer on June 13, 2026 at 11:25 am
    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
    

    or

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.