?
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
Medium Priority
?
355 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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 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