Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
278 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 52

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 52

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

604 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