Skip to content

Commit 6ba47dc

Browse files
authored
Merge pull request #10564 from haywoodsloan/9353-optimize-transfer-logins-sql
AB#9353: AI Optimization | Formatting | Transfer logins and passwords between instances of SQL Server
2 parents 495d29f + aad43c4 commit 6ba47dc

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

support/sql/database-engine/security/transfer-logins-passwords-between-instances.md

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
---
2-
title: Transfer logins and passwords between instances
3-
description: This article describes how to transfer the logins and the passwords between different instances of SQL Server running on Windows.
2+
title: Transfer SQL Server Logins and Passwords Between Instances
3+
description: Learn how to transfer logins and passwords between SQL Server instances to maintain user access and security during migrations or restores.
44
ms.date: 01/17/2025
55
ms.custom: sap:Security, Encryption, Auditing, Authorization
6-
ms.reviewer: jopilov
7-
ms.author: bartd
6+
ms.reviewer: jopilov, bartd, v-shaywood
87
ms.topic: how-to
98
---
109

1110
# Transfer logins and passwords between instances of SQL Server
1211

13-
This article describes how to transfer the logins and passwords between different instances of Microsoft SQL Server running on Windows. The instances might be on the same server or different servers, and their versions might differ.
14-
1512
_Original product version:_   SQL Server
1613
_Original KB number:_   918992, 246133
1714

15+
## Summary
16+
17+
This article describes how to transfer logins and passwords between different instances of [Microsoft SQL Server](/sql/sql-server/sql-server-technical-documentation) running on Windows. Use these procedures when you migrate databases between servers or need to maintain consistent user access across SQL Server instances. The instances can be on the same server or different servers, and their versions can differ.
18+
1819
## Why transfer logins between SQL Server instances?
1920

20-
In this article, server A and server B are servers.
21+
When you move a database to a new server (such as during a migration or restore), you move the database users, but their corresponding server-level logins might not exist on the new instance. This condition creates [orphaned users](/sql/sql-server/failover-clusters/troubleshoot-orphaned-users-sql-server). Transferring logins and passwords is crucial for maintaining security and access continuity.
2122

22-
After you move a database from a SQL Server instance on server A to a SQL Server instance on server B, users might be unable to log in to the database server on server B. Additionally, users might receive the following error message:
23+
After you move a database from a SQL Server instance on server A to a SQL Server instance on server B, users might be unable to sign in to the database server on server B. Additionally, users might receive the following error message:
2324

2425
> Login failed for user '**MyUser**'. (Microsoft SQL Server, Error: 18456)
2526
2627
This problem occurs because the logins from the SQL Server instance on server A don't exist in the SQL Server instance on server B.
2728

28-
Keep in mind that error 18456 occurs for many other reasons. For more information on these causes and their resolutions, see [MSSQLSERVER_18456](/sql/relational-databases/errors-events/mssqlserver-18456-database-engine-error).
29+
Error 18456 can also occur for several other reasons. For more information on the various causes and their resolutions, see [MSSQLSERVER_18456](/sql/relational-databases/errors-events/mssqlserver-18456-database-engine-error).
2930

3031
## Steps to transfer logins between SQL Server instances
3132

32-
To transfer the logins, use one of the following methods, as appropriate for your situation.
33+
To transfer logins, use one of the following methods, as appropriate for your situation.
3334

34-
### Method 1: Generate scripts via SSMS on the source server and manually reset passwords for SQL Server logins on the destination server
35+
### Generate scripts via SSMS on the source server and manually reset passwords for SQL Server logins on the destination server
3536

36-
You can generate login scripts in SQL Server Management Studio (SSMS) by using the [Generate Scripts option for a database](/sql/ssms/tutorials/scripting-ssms#script-a-database-by-using-the-generate-scripts-option).
37+
You can generate login scripts in SQL Server Management Studio (SSMS) by using the [Generate Scripts option for a database](/sql/ssms/tutorials/scripting-ssms#script-a-database-by-using-the-generate-scripts-option).
3738

3839
To generate scripts via SSMS on the source server and manually reset passwords for SQL Server logins on the destination server, follow these steps:
3940

@@ -44,21 +45,21 @@ To generate scripts via SSMS on the source server and manually reset passwords f
4445
1. Select **Next** to open the **Set Scripting Options** page.
4546
1. Select the **Advanced** button for Script Login options.
4647
1. In the **Advanced** list, find **Script Logins**, set the option to **True** and select **OK**.
47-
1. Back to **Set Scripting Options** under **Select how scripts should be saved** and select **Open in new query window**.
48+
1. Return to **Set Scripting Options**, under **Select how scripts should be saved** select **Open in new query window**.
4849
1. Select **Next** twice, and then select **Finish**.
4950
1. Find the section in the script that contains logins. Typically, the generated script contains text with the following comment at the beginning of this section:
5051

5152
`/* For security reasons the login is created disabled and with a random password. */`
5253

5354
> [!NOTE]
54-
> This indicates that the SQL Server Authentication logins are generated with a random password and are disabled by default. You need to reset the password and re-enable these logins on the destination server.
55+
> This comment indicates that SQL Server Authentication logins are generated with a random password and are disabled by default. You must reset the password and re-enable these logins on the destination server.
5556
5657
1. Apply the login script from the larger generated script to the destination SQL Server.
5758
1. For any SQL Server Authentication logins, reset the password on the destination SQL Server and re-enable those logins.
5859

59-
### Method 2: Transfer logins and passwords to the destination server (Server B) using scripts generated on the source server (Server A)
60+
### Transfer logins and passwords to the destination server (Server B) by using scripts generated on the source server (Server A)
6061

61-
1. Create stored procedures that will help generate necessary scripts to transfer logins and their passwords. To do so, connect to Server A using SQL Server Management Studio (SSMS) or any other client tool and run the following script:
62+
1. Create stored procedures that help generate necessary scripts to transfer logins and their passwords. To do so, connect to Server A by using [SQL Server Management Studio (SSMS)](/sql/ssms/sql-server-management-studio-ssms) or any other client tool and run the following script:
6263

6364
```sql
6465
USE [master]
@@ -275,7 +276,7 @@ To generate scripts via SSMS on the source server and manually reset passwords f
275276
> [!NOTE]
276277
> This script creates two stored procedures in the master database. The procedures are named **sp_hexadecimal** and **sp_help_revlogin**.
277278

278-
1. In the SSMS query editor, select the **[Results to Text](/sql/ssms/f1-help/database-engine-query-editor-sql-server-management-studio)** option.
279+
1. In the SSMS query editor, select the [Results to Text](/sql/ssms/f1-help/database-engine-query-editor-sql-server-management-studio) option.
279280

280281
1. Run the following statement in the same or a new query window:
281282

@@ -285,7 +286,7 @@ To generate scripts via SSMS on the source server and manually reset passwords f
285286

286287
1. The output script that the `sp_help_revlogin` stored procedure generates is the login script. This login script creates the logins that have the original Security Identifier (SID) and the original password.
287288
1. Review and follow the information in the [Additional considerations when transferring SQL Server logins](#additional-considerations-when-transferring-sql-server-logins) section before you proceed with implementing steps on the destination server.
288-
1. Once you implement any applicable steps from the [Additional considerations when transferring SQL Server logins](#additional-considerations-when-transferring-sql-server-logins) section, connect to the destination server B using any client tool (like SSMS).
289+
1. Once you implement any applicable steps from the [Additional considerations when transferring SQL Server logins](#additional-considerations-when-transferring-sql-server-logins) section, connect to the destination server B by using any client tool (such as SSMS).
289290
1. Run the script generated as the output of `sp_helprevlogin` from server A.
290291

291292
## Additional considerations when transferring SQL Server logins
@@ -294,41 +295,42 @@ Review the following information before you run the output script on the instanc
294295

295296
### Understand password hashing in SQL Server login transfers
296297

297-
- A password can be hashed in the following ways:
298+
You can hash a password in the following ways:
299+
300+
- `VERSION_SHA1`: SQL Server uses the SHA1 algorithm to generate this hash. Versions from SQL Server 2000 through SQL Server 2008 R2 use this hash.
301+
- `VERSION_SHA2`: SQL Server uses the SHA2 512 algorithm to generate this hash. Versions from SQL Server 2012 and later use this hash.
298302

299-
- `VERSION_SHA1`: This hash is generated by using the SHA1 algorithm and is used in SQL Server 2000 through SQL Server 2008 R2.
300-
- `VERSION_SHA2`: This hash is generated by using the SHA2 512 algorithm and is used in SQL Server 2012 and later versions.
301-
- In the output script, the logins are created by using the encrypted password. This is because of the `HASHED` argument in the `CREATE LOGIN` statement. This argument specifies that the password that is entered after the `PASSWORD` argument is already hashed.
303+
The output script creates the logins by using the encrypted password. The `HASHED` argument in the [CREATE LOGIN](/sql/t-sql/statements/create-login-transact-sql) statement causes this behavior. This argument indicates that the password entered after the `PASSWORD` argument is already hashed.
302304

303305
### Handle domain changes during SQL Server login transfers
304306

305-
Are your source and destination servers in different domains? Review the output script carefully. If server A and server B are in different domains, you have to change the output script. Then, you have to replace the original domain name by using the new domain name in the `CREATE LOGIN` statements. The integrated logins that are granted access in the new domain don't have the same SID as the logins in the original domain. Therefore, users are orphaned from these logins. For more information about how to resolve these orphaned users, see [Troubleshoot orphaned users (SQL Server)](/sql/sql-server/failover-clusters/troubleshoot-orphaned-users-sql-server) and [ALTER USER](/sql/t-sql/statements/alter-user-transact-sql).
307+
If your source and destination servers are in different domains, review the output script carefully. You must change the output script and replace the original domain name with the new domain name in the `CREATE LOGIN` statements. The integrated logins that are granted access in the new domain don't have the same SID as the logins in the original domain. Therefore, users become orphaned from these logins. For more information about how to resolve orphaned users, see [Troubleshoot orphaned users (SQL Server)](/sql/sql-server/failover-clusters/troubleshoot-orphaned-users-sql-server) and [ALTER USER](/sql/t-sql/statements/alter-user-transact-sql).
306308
307-
If server A and server B are in the same domain, the same SID is used. Therefore, users are unlikely to be orphaned.
309+
If server A and server B are in the same domain, the same SID is used. Therefore, users aren't orphaned.
308310

309311
### Required permissions to view and select SQL Server logins
310312

311-
By default, only a member of the sysadmin fixed server role can run a `SELECT` statement from the `sys.server_principals` view. Unless a member of the sysadmin fixed server role grants the necessary permissions to the users, the users can't create or run the output script.
313+
By default, only members of the [sysadmin fixed server role](/sql/relational-databases/security/authentication-access/server-level-roles) can run a `SELECT` statement from the `sys.server_principals` view. Unless a member of the sysadmin fixed server role grants the necessary permissions to other users, those users can't create or run the output script.
312314
313315
### Default database setting isn't scripted and transferred
314316

315-
The steps in this article don't transfer the default database information for a particular login. This is because the default database might not always exist on server B. To define the default database for a login, use the `ALTER LOGIN` statement by passing in the login name and the default database as arguments.
317+
The steps in this article don't transfer the default database information for a particular login. This limitation exists because the default database might not always exist on server B. To define the default database for a login, use the [ALTER LOGIN](/sql/t-sql/statements/alter-login-transact-sql) statement by passing in the login name and the default database as arguments.
316318
317319
### Manage sort order differences in SQL Server login transfers
318320
319-
There might be differences in sort orders between the source and destination servers, or they might be the same. Here's how each scenario can be addressed:
321+
The source and destination servers might have different sort orders, or they might use the same sort order. Here's how you can address each scenario:
320322

321-
- **Case-insensitive server A and case-sensitive server B**: The sort order of server A might be case-insensitive, and the sort order of server B might be case-sensitive. In this case, users must type the passwords in all uppercase letters after you transfer the logins and the passwords to the instance on server B.
322-
- **Case-sensitive server A and case-insensitive server B:** The sort order of server A might be case-sensitive, and the sort order of server B might be case-insensitive. In this case, users can't log in by using the logins and the passwords that you transfer to the instance on server B unless one of the following conditions is true:
323+
- **Case-insensitive server A and case-sensitive server B**: The sort order of server A is case-insensitive, and the sort order of server B is case-sensitive. In this case, users must type the passwords in all uppercase letters after you transfer the logins and the passwords to the instance on server B.
324+
- **Case-sensitive server A and case-insensitive server B:** The sort order of server A is case-sensitive, and the sort order of server B is case-insensitive. In this case, users can't sign in by using the logins and the passwords that you transfer to the instance on server B unless one of the following conditions is true:
323325
324326
- The original passwords contain no letters.
325327
- All letters in the original passwords are uppercase letters.
326328
327-
- **Case-sensitive or case-insensitive on both servers**: The sort order of both server A and server B might be case-sensitive, or the sort order of both server A and server B might be case-insensitive. In these cases, the users don't experience a problem.
329+
- **Case-sensitive or case-insensitive on both servers**: The sort order of both server A and server B is case-sensitive, or the sort order of both server A and server B is case-insensitive. In these cases, the users don't experience a problem.
328330

329331
### Resolve conflicts with existing SQL Server logins on destination server
330332

331-
The script is designed to check if the login exists on the destination server and create a login only if it doesn't. However, if you receive the following error message when you run the output script on the instance on server B, you have to manually resolve it by following the steps in this section.
333+
The script checks if the login exists on the destination server and creates a login only if it doesn't exist. However, if you receive the following error message when you run the output script on the instance on server B, you must manually resolve the conflict by following the steps in this section.
332334
333335
> Msg 15025, Level 16, State 1, Line 1
334336
> The server principal '**MyLogin**' already exists.
@@ -338,13 +340,13 @@ Similarly, a login that's already in the instance on server B might have a SID t
338340
> Msg 15433, Level 16, State 1, Line 1
339341
> Supplied parameter sid is in use.
340342
341-
To manually resolve the issue, follow these steps:
343+
To manually resolve the conflict, follow these steps:
342344
343345
1. Review the output script carefully.
344346
1. Examine the contents of the `sys.server_principals` view in the instance on server B.
345347
1. Address these error messages as appropriate.
346348
347-
Starting with SQL Server 2005, the SID for a login is used to manage database-level access. Occasionally, a login might have different SIDs when mapped to users in different databases. This issue can occur if databases are manually combined from different servers. In such cases, the login can only access the database where the database principal's SID matches the SID in the `sys.server_principals` view. To resolve this issue, manually remove the database user with the mismatched SID using the [DROP USER](/sql/t-sql/statements/drop-user-transact-sql) statement. Then, add the user again with the `CREATE USER` statement and map it to the correct login (server principal).
349+
Starting with SQL Server 2005, the SID for a login manages database-level access. Occasionally, a login might have different SIDs when mapped to users in different databases. This problem can occur if you manually combine databases from different servers. In such cases, the login can only access the database where the database principal's SID matches the SID in the `sys.server_principals` view. To resolve this problem, manually remove the database user with the mismatched SID by using the [DROP USER](/sql/t-sql/statements/drop-user-transact-sql) statement. Then, add the user again by using the `CREATE USER` statement and map it to the correct login (server principal).
348350

349351
For more information and to distinguish servers from database principals, see [CREATE USER](/sql/t-sql/statements/create-user-transact-sql) and [CREATE LOGIN](/sql/t-sql/statements/create-login-transact-sql).
350352

0 commit comments

Comments
 (0)