How to Fix Windows Orphaned Users on Sql Server

If you have been running a SQL Server for any length of time, chances are you have come across the issue of orphaned users. In simple terms, orphaned users are ones that are associated with logins that no longer exist on the server, making it impossible to authenticate them. This issue not only affects the security of your databases but can also cause errors in your applications. In this blog post, we will take a look at how to fix the orphaned user issue on SQL Server.

Video Tutorial:

Why You Need to Fix Orphaned Users on SQL Server

Orphaned users are a problem because they can’t be authenticated due to a missing login. This means that they can’t access the databases, which can result in errors in your applications. Additionally, orphaned users can be a security risk if someone gets hold of an orphaned login. It’s important to regularly check for orphaned users to keep your SQL Server secure and running smoothly.

Method 1: Using sp_change_users_login

The sp_change_users_login system stored procedure is used to map a user in the current database to a login in SQL Server. The steps to use this method are as follows:

1. Open SQL Server Management Studio (SSMS) and connect to your server.
2. Open a new query window.
3. Type the following command in the query window:

"`
USE [YourDatabaseName] GO
sp_change_users_login @Action=’Report’
"`

4. This command will generate a list of all the orphaned users in the database.
5. To fix the orphaned user issue, type the following command in the query window:

"`
USE [YourDatabaseName] GO
sp_change_users_login @Action=’update_one’, @UserNamePattern=’YourUserName’, @LoginName=’YourLoginName’
"`

Replace `YourDatabaseName` with the name of your database, `YourUserName` with the name of the orphaned user, and `YourLoginName` with the name of the login you want to map it to.

Pros:
– Quick and easy to use.
– Can map multiple users to the same login.
– Provides a report of all orphaned users.

Cons:
– Requires some knowledge of SQL Server.
– Can only map one user to a login at a time.
– Relies on stored procedures.

Method 2: Using SQL Server Management Studio

SQL Server Management Studio (SSMS) provides a graphical user interface for managing databases, including fixing orphaned users. The steps to use this method are:

1. Open SSMS and connect to your server.
2. Expand the databases node in the Object Explorer.
3. Right-click on the database that has the orphaned user and select Properties.
4. Click on the Files page on the left-hand side.
5. Take note of the logical name of the database.
6. Click on the User Mapping page.
7. Check the box next to the username that is orphaned.
8. In the Login section of the page, select the login you want to map the user to.
9. In the Default Schema section, select the schema you want the user to be associated with.
10. Click OK to save the changes.

Pros:
– Easy to use for those not familiar with SQL Server.
– Can map multiple users to a login at once.
– Provides a graphical interface.

Cons:
– Can be slow for large databases.
– Limited mapping options compared to sp_change_users_login.
– Can generate errors if not used correctly.

Method 3: Using PowerShell

PowerShell is a powerful scripting tool that can be used to automate common administrative tasks in SQL Server, including fixing orphaned users. The steps to use this method are:

1. Open PowerShell.
2. Type the following command to connect to your SQL Server instance:

"`
$serverInstance = New-Object Microsoft.SqlServer.Management.Smo.Server("YourServerName")
"`

3. Type the following command to get the database object:

"`
$db = $serverInstance.Databases["YourDatabaseName"] "`

4. Type the following command to get the user object:

"`
$user = $db.Users["YourUserName"] "`

Replace `YourServerName` with the name of your server, `YourDatabaseName` with the name of your database, and `YourUserName` with the name of the orphaned user.

5. Type the following command to assign a new login to the user:

"`
$user.Login = "YourLoginName"
"`

Replace `YourLoginName` with the name of the login you want to map it to.

6. Type the following command to update the user:

"`
$user.Alter()
"`

Pros:
– Can be automated and scripted for large databases.
– Provides more control over mapping options.
– PowerShell is a powerful tool that can be used for other administrative tasks.

Cons:
– Requires knowledge of PowerShell.
– Mapping process can be time-consuming for multiple users.
– Can generate errors if not used correctly.

What to Do If You Can’t Fix Orphaned Users on SQL Server

If you are unable to fix the orphaned user issue using the methods described above, there are a few things you can do. Firstly, you can restore a backup of the database from a time before the orphaned user issue occurred. Alternatively, you can create a new user and login, map the user to the login, and then transfer the permissions from the orphaned user to the new user.

Bonus Tip

Regularly monitoring your SQL Server for orphaned users can help prevent security risks and errors in your applications. Set up alerts to notify you when orphaned users are detected.

5 FAQs

Q1: What causes orphaned users?

A: Orphaned users can be caused by a variety of things, including deleting a login that is associated with a user, restoring a database from a backup that was created on a different SQL Server instance, or migrating a database to a new instance.

Q2: Can orphaned users access my database?

A: Orphaned users cannot access your database as they are not associated with a valid login.

Q3: How do I check for orphaned users?

A: The sp_change_users_login system stored procedure or SQL Server Management Studio can be used to check for orphaned users.

Q4: Can I use these methods on all versions of SQL Server?

A: Yes, these methods can be used on all versions of SQL Server.

Q5: What happens to the user’s permissions when I map them to a new login?

A: The user’s permissions remain intact when mapping them to a new login.

Final Thoughts

Fixing orphaned users on SQL Server is an important task that can help keep your databases and applications running smoothly. Whether you choose to use sp_change_users_login, SQL Server Management Studio, or PowerShell, it’s important to regularly monitor your databases for orphaned users and take action as necessary.