Solved

Avoiding "The server principal ... is not able to access the database ... under the current security context" runtime on 'USE [db]', how?

Posted on 2013-05-16
1
348 Views
Last Modified: 2013-05-17
In my code, I gather all database names and tables from a database server. However, there are cases where my User ID does not have the permission to "USE [database]". It would be too late because I will get an exception and some do not favor code that use exception handlers.

Here is the runtime error again:
"The server principal ... is not able to access the database ... under the current security context" ...

Is there an SQL query that I could use before issuing "USE [database]" that determines whether or not I could really "USE [database]" without getting the runtime error above?
0
Comment
Question by:rodqxo
[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
1 Comment
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39172456
the below one should help you

it will return 1 if you have permissions,replace <db_name> with the database name you are going to use with USE directive
SELECT HAS_PERMS_BY_NAME('<db_name>', 'DATABASE', 'ANY');
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

696 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