0

I have a login1 and two users user1 and user2. All three are created previously. Originally, login1 was mapped with user1 and now I want to change to map with user2.

First, I tried

ALTER USER user2 WITH LOGIN = login1;

But SSMS shows

Cannot remap user to login 'login1', because the login is already mapped to a user in the database.

Then I tried

EXEC sp_change_users_login 'Update_One','user2','login1'

It shows

The login already has an account under a different user name.

When I search with these error messages, most of the posts are talking about why this would happen or how to identify the related (origin) user?

But my focus is as titled, how can I change from one existing user to another for an existing login? Is it possible to achieve without re-creating any of them?

asked May 10, 2023 at 5:52

2 Answers 2

1

how can I change from one existing user to another for an existing login

I intend to leave user1 orphaned

Create a new login and map user1 to the new login before mapping user2 to login1.

eg

create login login1 with password = '09)(*)(4u239u4s'
create login login2 with password = '09)(*)(4u239u4s'
create user user1 for login login1
create user user2 for login login2
go
create login newlogin with password='2348(*-09udjA'
alter user user1 with login=newlogin
alter user user2 with login=login1
drop login newlogin --user1 is now orphaned
answered May 10, 2023 at 18:05
1

You try to change the user to the desired login. This is fine.

But that login already have a user in a database. A login cannot be "mapped" to two users in a database.

One option is to not remap the login.

The other option is to remove that user to which your login is currently mapped to.

answered May 10, 2023 at 6:48
7
  • when you said "remove that user", you mean DROP USER user1? Commented May 10, 2023 at 6:51
  • Is there a way not to drop and recreate it? Commented May 10, 2023 at 6:52
  • You have to decide which if the two usrs you want mapped to that login. You can't have two mapped to the same login. Think about it, and it makes sense. I can't really say more without knowing more about your situarion. Commented May 10, 2023 at 7:15
  • I intend to leave user1 orphaned Commented May 10, 2023 at 7:28
  • 2
    There's no option to unmap a user, I'm afraid. That would be something like WITH NO_LOGIN in the ALTER USER command. But the documentation doesn't have anything like that. You can suggest this to Ms, but be sure to include your scenario. As for now, take a step back and see if you can avoid this requirement in the first place. Commented May 10, 2023 at 9:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.