Solved

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

Posted on 2016-10-31
3
125 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 50

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 50

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 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