Solved

linkedserver errors when you drop and add it

Posted on 2012-03-23
1
542 Views
Last Modified: 2012-03-26
Hi,

my script contains

USE MyDB
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')
begin
          Drop user linkedserver
end

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

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~
0
Comment
Question by:IzzyTwinkly
1 Comment
 
LVL 15

Accepted Solution

by:
Deepak Chauhan earned 500 total points
ID: 37760105
Hi

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

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

result will be >>> "db_owner"

then run this

ALTER AUTHORIZATION ON SCHEMA::db_owner TO dbo;

go
drop user linkedserver.

>>
0

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

840 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