How to connect an existing SQL Server login to an existing SQL Server database user of same name

SqlSql Server

Sql Problem Overview


Is there a SQL Server command to connect a user of a single database to a login for the database server of the same name?

For example:

>Database Server - Default Instance
Database: TestDB

>Server Login - TestUser
Existing user on TestDB - TestUser

If I try to make the TestUser login a user of the TestDB database, the "User, group, or role" already exists.

Does anybody know of an easy way to assign the DB user to the server login?

Sql Solutions


Solution 1 - Sql

The new way (SQL 2008 onwards) is to use ALTER USER

ALTER USER OrphanUser WITH LOGIN = correctedLoginName;

Solution 2 - Sql

I think it's sp_change_users_login. It's been a little while, but I used to do that when detaching and reattaching databases.

Solution 3 - Sql

I've used the sp_change_users_login procedure to accomplish, I think, that scenario.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjkelleyView Question on Stackoverflow
Solution 1 - SqlgbnView Answer on Stackoverflow
Solution 2 - SqlGratzyView Answer on Stackoverflow
Solution 3 - SqljoeltView Answer on Stackoverflow