?
Solved

The server principal "(username)" is not able to access the database "(databasename)" under the current security context

Posted on 2016-10-31
3
Medium Priority
?
195 Views
Last Modified: 2016-10-31
I'm trying to access a database in SQL Server 2012

Microsoft SQL Server 2012 - 11.0.5343.0 (X64)
      May  4 2015 19:11:32
      Copyright (c) Microsoft Corporation
      Standard Edition (64-bit) on Windows NT 6.3 <X64> (Build 14393: )

but I'm getting the error:
08004:[Microsoft][ODBC SQL Server Driver][SQL Server]The server principal "(username)" is not able to access the database "(databasename)" under the current security context.

I had fixed this in the past with going to SSMS > Security > Logins > (user) > User Mapping and mapping the user to the database, but when I do so I get this message:
Create failed for User (username)
User, group or role (username) already exists in the current database.

What should I do to get access?
0
Comment
Question by:purplesoup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41866686
I'm trying to access a database in SQL Server 2012
With SSMS or any other tool?
1
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 41866689
In the other hand this can be an orphaned user issue. Did you restore the database lately?
If so then check for orphaned users. The following command returns the orphan users from a database:
USE databasename
GO
sp_change_users_login 'report'

Open in new window

Then with the reported orphan users you'll need to run the following command for each of those users:
sp_change_users_login 'update_one', '<LoginName>', '<DBUserName>'

Open in new window

1
 

Author Closing Comment

by:purplesoup
ID: 41866737
Thanks Vitor that did it - I am trying to create a copy of a database for testing, I have moved some code out of a desktop application and onto the web and want to run the same process on each database, so I backed up the database and restored it to a new database, but it doesn't seem to restore the users of the first database with the same permissions in the second.

Basically I want two identical databases and run the same process on each - one from the web, the other from the desktop, and check the database results are the same.

Thanks for your help. In fact perhaps I will post a second question about this.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question