How to fix orphan user in SQL server? 

Introduction
In SQL Server, an orphaned user occurs when a database user exists without a corresponding login in the server’s master database. This usually happens after restoring a database from another server or after a login is deleted. Orphaned users can cause permission issues because the database user is no longer linked to a valid SQL Server login, preventing authentication or access to the database. Identifying and fixing orphaned users is an important part of SQL Server maintenance to ensure smooth application and user access. 

 

How to fix orphan user in SQL server? 

 There are five ways to fix the orphan user  

1)AUTO_FIX 

Database user is mapped with same named SQL server login it may also create login if not present 

Reports of orphan user 

If the login exists and both SQL login name and username are same 

Exec sp_change_user_login ‘autofix’,’username’ 

 

 

If the login does not exist, the below stored procedure automatically creates a SQL login with a name, same as database username 

Exec sp_change_user_login ‘auto_fix’,’username’, null,’paswword’ 

 

2)update_one 

Login exists already or login is created in a server 

Login name and user name may or may not same and also there is a Sid mismatch 

It links the specified database users to an existing SQL server login 

Exec sp_change_user_login ‘update_one’,’username’,’login name’ 

It links the specified database users to an existing sql server login 

 

 

 

3)creating login with user Sid 

Login does not exist or not created before 

Create login orphan2 with password =’123’ sid= 0x73DE3B9F9D6A3D43BA54683EE627A4F7 

 4)sp_help_revlogin 

 Run the code in source server 

Execute exec sp_help_revlogin 

Copy the login script for the users 

Paste it in user database in destination server 

 

Conclusion
Fixing orphaned users in SQL Server is important to maintain smooth database access. It ensures the link between server logins and database users is intact, preventing permission errors. Orphaned users often appear after backups, restores, or migrations, and addressing them promptly helps avoid disruptions. Regular checks also improve security and keep the database running without access problems. 

 

 

Recent Posts