Click here to Skip to main content
Click here to Skip to main content

Few Step(s) to Remove SQL Server Database User(s)

By , 20 Mar 2010
 

Introduction

Database security is one of the significant concerns for most DBAs. DBAs frequently restore or backup the database, this is a very common scenario, But the thing is after successfully restoring a new version of your database, you want to remove the current users. Probably you thought of just expanding the user node and deleting the desire user; in that sense you are somewhat correct. But if you face an error like:

Msg 15421, Level 16, State 1, Line 1
The database principal owns a database role and cannot be dropped.	 

What will you do?

Microsoft SQL Server provides quite a lot of ways to maintain the security of database. This article is not about the security of Microsoft SQL server.

In this article, I will try to explain how to resolve the following issues:

  1. The database principal owns a database role and cannot be dropped.
  2. The database principal owns a schema and cannot be dropped.

Solution

I try to categorize into two sections, section-A; we will discuss to find out the list of roles in which the user exists and the section-B; we will discuss how to resolve it.

Section-A

In this section, our primary goal is to find out the list of existing roles of our target database. For this purpose, we use a simple transact-SQL with the help of SQL Server SYS.DATABASE_PRINCIPALS table. A sample SQL script and the required step(s) are listed below:

  1. Open SQL Server Management Studio and login as an admin user.
  2. Select the database, set the user name & execute the following transact-SQL for getting the database role and user detail.

Sample SQL Script

SELECT     DBPRINCIPAL_1.NAME AS ROLE, DBPRINCIPAL_1.NAME AS OWNER
FROM         SYS.DATABASE_PRINCIPALS AS DBPRINCIPAL_1 INNER JOIN
                 SYS.DATABASE_PRINCIPALS AS DBPRINCIPAL_2 
		    ON DBPRINCIPAL_1.PRINCIPAL_ID = DBPRINCIPAL_2.OWNING_PRINCIPAL_ID
WHERE     (DBPRINCIPAL_1.NAME = 'User Name To Remove')  

The above transact-SQL returns a list of roles in which the user exists.

More information on "SYS.DATABASE_PRINCIPALS " table can be found at this link.

Section-B

I think this is not a very intricate task, let’s start, your SQL Server Management Studio is open and you are logged in as an admin user i.e., “sa”.

From the section-A we already get the list, now the task is to remove the desired user. To do this, we need to follow the step(s) listed below:

  1. Now expand Databases node from object explorer.
    1. Select the target Database >>---> Security >>--> Roles >>--> Database Roles.
    2. Now double click the entries that were listed in the output of the above SQL command.
    3. Change the “Owner” to some temp username.
    4. If the username you want to delete appears in the dialog box, select and remove it from there too.
      (Do this for all the Roles that came up in the above SQL query.)
  2. Navigate to Databases >>--> the target Database >>--> Security >>--> Schemas.
    1. Double-click to open “db_owner” and change the schema owner to dbo.
  3. Now go to: Databases >>--> Target Database >>--> Security >>--> Users.
  4. Right click the username you want to delete and click “Delete”, then click OK in the new dialog box that appears.

Note: You can also try the stored procedure sp_dropuser after accomplishing the step(s) above except section-B step 4.

Example

EXEC sp_dropuser 'User name' 

Conclusion

I hope this might be helpful to you. Enjoy!

Reference

  • MSDN

History

  • 20th March, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Md. Marufuzzaman
CEO
Bangladesh Bangladesh
Member
He is the founder & CEO of MNH Technologies and working for urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc. He has over ten years of professional experiences in design and developing Client-Server, Multi-Tier, Database, Web based business software solutions, Enterprise Applications, API, WebAPI, Google Analytics implementation, Add-In, Documentation & Technical Writing etc for Windows / Mac using Microsoft SQL Server, Oracle, MySql, PS, C#, VB.NET, ASP.NET, PHP, RoR, Visual Basic etc. He has also more than two years experience in Mobile-VAS (Platform Development).
 
He worked for various software development & technology consulting. His core focus on technologies to create dynamic data-driven systems that add value to your business and dynamic technology consulting that builds advanced solutions for the industries across the various vertices.
 
He also work as a Solution Architect at Dhrupadi Techno Consortium Limited (DTCL) and responsible for analyzing business requirements and offered optimum solutions (multiple options), which would address all current requirements, provide flexibility for future growth and allow smooth transition between old system and new system.
 
He graduated with honors from The University of Asia Pacific, in Computer Science and Engineering. He was awarded as “Most Valuable Professional” (MVP) at 2010 and 2011 by CodeProject.com and also selected as a Mentor of CodeProject.com
 
Specialties: Software Development Management, System Integration, Data Warehouse Architecture, Virtualization.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralSome alternativememberaldo hexosa13 Dec '10 - 17:14 
GeneralRe: Some alternativemvpMd. Marufuzzaman5 Oct '11 - 23:02 
GeneralMy vote of 1memberPeter_Bernheim22 Mar '10 - 23:22 
GeneralRe: My vote of 1mvpMd. Marufuzzaman22 Mar '10 - 23:54 
GeneralMy vote of 5memberdenic25 Mar '10 - 2:47 
GeneralHi comanionmemberAnil Srivastava21 Mar '10 - 21:15 
GeneralRe: Hi comanionmvpMd. Marufuzzaman21 Mar '10 - 21:58 
GeneralGood workmemberMohd Arshad (Sam)19 Mar '10 - 10:28 
GeneralRe: Good workmvpMd. Marufuzzaman19 Mar '10 - 16:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 21 Mar 2010
Article Copyright 2010 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid