title: Generalized query to find whether the database user owns a schema
body: <p>We have a lot of DBUsers in our database. We have to clean up all those users from the database. When I tried to drop the user from the DB it failed due to the following error</p>
  
  <blockquote>
    <p>Msg 15138, Level 16, State 1, Line 2
    The database principal owns a schema
    in the database, and cannot be
    dropped.</p>
  </blockquote>
  
  <p>So I found the solution for this i.e I changed the ownership of the schema to <code>dbo</code>. Now I can drop the user by using below script</p>
  
  <pre><code>SELECT name FROM  sys.schemas WHERE principal_id = USER_ID('myUser')
  ALTER AUTHORIZATION ON SCHEMA::SchemaName TO dbo 
  GO
  DROP USER myUser
  </code></pre>
  
  <p>But I want to drop a number of users so I need to generate generalized script.
  I am using SQL Server 2008</p>