01/01/2015 by Nitesh

Solution: The server principal is not able to access the database under the current security context in SQL Server

Friends,

Yesterday I backed up a SQL database and restored it on a different server. A user was associated with the database on the original server and after I restored it on the new server, the same user was unable to use the database. Trying to use the database gave the error – “The server principal is not able to access the database under the current security context in SQL Server.” On doing a bit of Bing related to the error, I found that this happens when a database is restored with users associated with them because the SID in “sys.sysusers” is not mapped to the SID present in “sys.syslogins”. These users are also known as “Orphaned Users“. To fix this issue, we need to execute the following SQL command in Management studio –

USE MyDB
GO
EXEC  sp_change_users_login @Action='update_one', @UserNamePattern='sqluserName',@LoginName='sqluserName'
GO

Here, MyDB is the name of the database and sqluserName is the name of the user mapped with database MyDB.

Hope this post helps you. Keep learning and sharing! Cheers!

#SQL#SQL Scripts#SQL Server