I have a couple of databases on my server, each with its own users. Now I need to find and delete all those logins, which are not mapped to any user in any database. But I've also seen that it is possible that logins are not mapped to any user but they are able to do some operations because they have roles like sysadmin. So in this case those logins are in use. I want to know how can I find really not useful logins which are just left orphaned without any use?
Thanks
Best Answer
Your case looks same as this.
However, following query help you to get logins that are not mapped to any user in the database and not assigned to server role, you may comment (--) the last predicate (
and (r.name = 'public' or r.name is null )
) in thewhere
clause to list all logins with their role names that are not mapped with any database user, and pickFixCommand
column value (T-SQL) for selected logins from result.