Not able to access database - but what object is being sought?

  • Hello experts,

    I'm getting reports of a curious error. It is the following:

    The server principal ""[principal]"" is not able to access the database ""[database]"" under the current security context.

    What makes it curious for me is that I am having trouble tracking down what object the principal (db user) is trying to access. I can't find any reference to the database in question in the code from which the error is being thrown. I've tried searching not only the immediate stored procedure code but also references to the database throughout the db server's SQL codebase (triggers, views, etc.), to no avail. I can't find where this user needs to access the database that would result in the error indicated.

    Does anyone know how to trace this kind of dependency? I don't want to just start granting permission to the database without knowing why the permission is needed.

    Thanks for any help.

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Has the user been mapped to the database? If not, that's most likely the cause of your error. If you're concerned about granting permissions. If the user needs to select or run procedure, they will need to be granted SELECT and EXECUTE at a minimum

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • So how does that piece of code look like? What line does the error message point to?

    By chance, does the procedure have an EXECUTE AS clause?

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • MyDoggieJessie (2/28/2014)


    Has the user been mapped to the database? If not, that's most likely the cause of your error. If you're concerned about granting permissions. If the user needs to select or run procedure, they will need to be granted SELECT and EXECUTE at a minimum

    Thanks - but that's just it. I can't find the relevant code that shows why the login needs this particular database (call it DB1). The error shows up in a web script. When I look at that line, it refers to a stored procedure call in a different database (call it DB2). There are also some views referenced in the stored procedure, but none of those views has a reference to DB1. I've also tried checking triggers on any of the tables called in the procs or views. No reference to DB1 there, either.

    Do you know where I might be failing to look to confirm the dependency on this database?

    Thanks again!

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Erland Sommarskog (2/28/2014)


    So how does that piece of code look like? What line does the error message point to?

    By chance, does the procedure have an EXECUTE AS clause?

    Hello Erland, thanks for your reply. Please see my other reply for a schematic description of the error and code. There is no EXECUTE AS statement in the procedure. But the error line number does fall right next to a procedure call in the web script. I tried checking that procedure, but it's in a different database and doesn't directly reference the database in the error. But maybe some dependent object somewhere along the line does reference the db. I just can't find it yet.

    Thanks again!

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Sounds like the front end web application is using a specific user account that does not have access (or no longer has access) to the database it's trying to reach. In some cases with web apps, they may be using an account, that yet uses another account to actually make a connection to the database from their business layer (sort of like a proxy account).

    It could also be a scenario where the procedure is calling another procedure, or function, or view, etc in another database that the web account can't reach...

    Your web developers need to do some digging to let you know exactly which account is being used, and which procedure is being called. It shouldn't be very difficult for them to pinpoint.

    ______________________________________________________________________________Never argue with an idiot; Theyll drag you down to their level and beat you with experience

  • You say that the error is on a certain line in a web script where there is a call to a stored procedure which is in "another database".

    But is the error with that call itself? Or does the procedure start executing and then runs into this error? An error message from SQL Server includes both procedure name and line number. If the line number is 0, the error is with invocation of that procedure. But of course, if you are looking at this from the web application, the web application may careless drop the information on the floor.

    One way to investigate this is to do:

    EXECUTE AS LOGIN = 'WebScriptUser'

    go

    EXEC storedproc @par1, @par2, ...

    go

    REVERT

    This should give you a clearer error message you can work from.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply