Solved

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

Posted on 2016-10-31
3
50 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
  • 2
3 Comments
 
LVL 46

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 46

Accepted Solution

by:
Vitor Montalvão earned 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

937 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

Need Help in Real-Time?

Connect with top rated Experts

3 Experts available now in Live!

Get 1:1 Help Now