linkedserver errors when you drop and add it

Posted on 2012-03-23
Last Modified: 2012-03-26

my script contains

Drop user linkedserver
Create User linkedserver For Login linkedserver
Exec sp_addrolemember 'db_owner', 'linkedserver

Then I got the following error:
-Cannot drop the user 'linkedserver', because it does not exist or you do not
have permission.

I changed my script to
IF EXISTS(SELECT * FROM sys.database_principals WHERE name = 'linkedserver')
          Drop user linkedserver

and then to
IF EXISTS(SELECT * FROM sys.database_principals WHERE name = 'linkedserver')
          Alter authorization on schema::linkedserver to dbo
          Drop user linkedserver

However, it seemed to work for one db but caused another error on a different db.
The errors I am getting are:
-Cannot drop the user 'linkedserver', because it does not exist or you do not
have permission.
-The database principal owns a schema in the database, and cannot be dropped.
-User, group, or role 'linkedserver' already exists in the current database.
-Cannot find the schema 'linkedserver', because it does not exist or you do not
have permission.

How should I reconstruct my script to resolve all the issues I stated above at the same time?
Plz help~
Question by:IzzyTwinkly
1 Comment
LVL 15

Accepted Solution

deepakChauhan earned 500 total points
ID: 37760105

use this script it will be work for you....

FROM sys.schemas s
WHERE s.principal_id = USER_ID('linkedserver');

result will be >>> "db_owner"

then run this


drop user linkedserver.


Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now