I have just moved a database from our test server to the live server by creating an empty database and restoring a backup to it.
Despite adding the Login correctly, I am getting the following error:
The server principal “myUser” is not able to access the database
“myDatabase” under the current security context.
I have tried clicking F7 in Object Explorer Details with no luck. I can get the error simply by running:
> USE myDatabase
> GO
>
> EXECUTE AS LOGIN = 'myUser'
Running the following script (from a related question) shows the user correctly
select princ.type_desc
, princ.name
, perm.permission_name
, perm.state_desc
, object_name(perm.major_id)
from sys.database_permissions perm
left join
sys.database_principals princ
on princ.principal_id = perm.grantee_principal_id
SQL_USER myUser CONNECT GRANT
Any suggestions???
Try using sp_change_users_login with the “Auto_Fix” action to resync the logins between the server and the database.