Thursday, December 6, 2012

Drop Failed for User in SQL server

Sometimes its irritating getting the same error again and again, where we feel we cant anything more than that.
When ever trying to delete a user from the database, you will get to see this error below.

The database principal owns a schema in the database, and cannot be dropped. (Microsoft SQL Server, Error: 15138).

The error itself is the meaning. It can't be dropped as it owns a schema.
Solution : -- Simple.. :)

Go to the security under the Database where you wanted to delete the user from,
goto users --> Right Click on the username--> select Properties

Now you can see clearly in General Tab
                                  Schemas Owned by this user:
          So please note the selected 'schemas' here, It doesn't matter if it is greay/inactive.
Now exit out from that window. 

(OR)
Simple Query : 
                         SELECT *, 'UserName' as ownerName
                         FROM sys.schemas s
                         WHERE s.principal_id = USER_ID('UserName')

Now you have to do the following :
Go to Schamas under security, select the schamaName which you have noted down above or from the query result. Right Click on it and select the properties. Now you can see the that same username placed as Schema Owner in General Tab
                                                Replace that username with dbo and click OK button.

Do the same for all the schema's you have noted above.
Now you can be able to delete the user.

You have other processes to do this, This is just one of them.

Please give me your feed back. New ideas most welcome.

2 comments: