Site icon Database Tutorials

Tip: “sp_validatelogins” and “sp_change_users_login”(Orphaned Users in SQL Server)

Detect Orphaned Windows Logins(sp_validatelogins)

With this system SP, you can identify Windows Logins that have been deleted from Active Directory on a SQL Server Instance.

You can run SP under the “master” database, if you have a Windows Login deleted from Active Directory, then this SP returns a result as follows:

This Login is not linked to any Active Directory user, so it does not work any good, so it can be easily deleted.

For more information about this SP, read Books Online:http://msdn.microsoft.com/en-us/library/ms181728.aspx

You can also compare this SP to the “sp_change_users_login” system SP. With sp_change_users_login, you can also identify orphaned SQL Users at the database level and map them to their Login.

What is Orphaned Users in SQL Server

If you create a login and map this login to a database, sql server creates a user in that database for mapping loggins and users. And when you delete the login mapped to that database, sql server doesnt delete the user in the database. So this user in the database no longer related to with a login in the instance. its orphaned now.

Find Orphaned Users in SQL Server(sp_change_users_login)

To detect orphaned SQL users in a database, you can run a command as follows:

If a result returns, then this means that there are orphaned SQL Users in that database.

sp_change_users_login is not working

Since sp_change_users_login is depreceated, it may not return any result for your system. You can use below script. I stole this script from below link.

https://www.mssqltips.com/sqlservertip/2624/script-to-find-and-drop-all-orphaned-users-in-all-sql-server-databases/

If you dont want to deleted orphaned users and match a login with the orphaned user you should use below script. I suggest using ALTER statement instead of sp_change_users_login due to it is depreceated.

In SQL Server 2008 and later versions, you can do the same with the following command:

In summary, sp_validateusers is used to detect orphaned Windows Logins; sp_change_users_login can also be used to identify orphaned SQL Users.

Exit mobile version