Deleting server logins does not delete the database users associated with the logins

Problem

When you delete a login using SSMS, a message will show up:
"Deleting server logins does not delete the database users associated with the logins. To complete the process, delete the users in each database. It may be necessary to first transfer the ownership of schemas to new users. "



If you have multiple databases, then it will take some time to manually check each database and find out all the database user associated with the login you just deleted.

Solution

How to find all the database user associated with the login to be deleted? A cursor will do, however there is a better way: stored procedure sp_MSforeachdb. The following example will find all the database user associated with the server login "test4".  

exec sp_msForEachDb ' use [?] 
select db_name() as [database_name], [name] as [user member] 
from  
sys.database_principals p  
where p.name = ''test4'''


1 comment:

  1. but it is not a answer to delete the user from the databases please provied the end solutions to delete the user

    ReplyDelete