Generic query to determine if a database user owns a schema

There are many DBUsers in our database. We must clear all these users of the database. When I tried to reset the user from the database, he failed due to the following error

Msg 15138, Level 16, State 1, Line 2 The principle of the database has a schema in the database, and cannot be discarded.

So, I found a solution for this. I changed the ownership of the scheme to dbo. Now I can refuse the user using below script

SELECT name FROM  sys.schemas WHERE principal_id = USER_ID('myUser')
ALTER AUTHORIZATION ON SCHEMA::SchemaName TO dbo 
GO
DROP USER myUser

But I want to abandon several users, so I need to create a generic script. I am using SQL Server 2008

+5
source share
1

, :

SELECT db.name AS [DB User], s.name AS [Schema]
FROM sys.database_principals db
JOIN sys.schemas s ON s.principal_id = db.principal_id
+7

All Articles