Solved

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

Posted on 2016-10-31
3
27 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 45

Expert Comment

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

Accepted Solution

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ssms - object execution statistics 12 37
how to fix this error 14 45
SQL server 2008 SP4 29 31
Test a query 23 10
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now