One thing that I have always hated more than anything in MS SQL Server is the way to ensure security. The security context constantly switches if you look at the server funny, and it is often very difficult (for me anyway) to predict or debug.
In connection with the problem today, however, I want: "I would like to add a line to my code that displays the security context that SQL Server uses when running this code." Is there such a team? For example, SELECT security_context()
To be a little clearer ... if I am in a stored procedure, and therefore I must obey the security context of the SP owner, I would like to see this. If I am in the code called by sp_executesql, and this will cause the security to be in the context of the SQL Server service account, I would like to see this.
At least I could understand why SQL Server believes that I should not have access to anything.
Thanks!
Example
-- Set up CREATE USER Test_User WITHOUT LOGIN CREATE TABLE Test_Security_Context (my_id INT) INSERT INTO Test_Security_Context VALUES (1) DENY SELECT ON Test_Security_Context TO Test_User GO CREATE PROCEDURE Test_Security_Context_SP AS SELECT SUSER_SNAME() SELECT * FROM Test_Security_Context -- This will return ok EXEC('SELECT SUSER_SNAME(); SELECT * FROM Test_Security_Context') -- SUSER_SNAME() will match above but select fails GO GRANT EXECUTE ON Test_Security_Context_SP TO Test_User GO -- Switch to the new user SETUSER 'Test_User' GO -- Do the test EXEC Test_Security_Context_SP GO -- Clean up SETUSER DROP PROCEDURE Test_Security_Context_SP DROP TABLE Test_Security_Context DROP USER Test_User GO
source share