Solved

linkedserver errors when you drop and add it

Posted on 2012-03-23
1
546 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

617 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