I Create a user in my database with following command :
CREATE USER Test WITHOUT LOGIN
I created a login with the Test Name.
I want to link Test user with Test Login.
When I use following command :
EXEC sp_change_users_login 'Update_One', 'Test', 'Test'
SQL Server raised bellow error :
Msg 15291, Level 16, State 1, Procedure sp_change_users_login, Line 114
Terminating this procedure. The User name 'Test' is absent or invalid.
And when I use following command :
ALTER USER Test WITH LOGIN = Test
SQL Server Raised Bellow error :
Msg 33016, Level 16, State 1, Line 2
The user cannot be remapped to a login. Remapping can only be done for users that were mapped to Windows or SQL logins.
How can I link a Test user with Test login.
My purpose of this link :
I want to restore a database from a server to another server and after complete restoring, map users with existing login on the server.
thanks in advance.
Best Answer
A user that was explicitly created with
WITHOUT LOGIN
cannot be mapped to a login after the fact.A user that was created with a login and then lost its association, e.g. because the database was moved to another server, can be remapped by first creating a new login (any name) and then using the
ALTER USER ... WITH LOGIN = ...
command.To demonstrate I am going to create a database
tst
and then create a logintstlogin
and an associated usertstuser
withintst
:To show correct association we can use the following query:
This results in
Now we will drop the login
tstlogin
and recheck our user:
which does not show an associated login anymore:
Next step is to create a new login. (I chose a diffenert name for demonstration purposes, but it could be the same name too.)
To associate that new login with our user we can use the
ALTER USER
statement:Checking the association once again:
shows that the
ALTER
statement was successful:If you actually need to change a "
WITHOUT LOGIN
" user to be a "with login" user, you will have to drop that user and recreate it. Make sure in that case to script out the permissions first, as dropping a user causes SQL Server to forget those.