Select permission 2

Hram Admin 80 Reputation points
2025-03-04T08:40:36.6933333+00:00

Hello!

Please excuse me for almost the same question as "Select permission" but there's one thing I'd like to be cleared up:

I see that I can't query a user database (at least the databases for which the Guest account is disabled) until the user mapping between an sql login and a db user is created...

Q1

...but what I still do not understand is why does the fn_my_permissions function applied to a user db (Resources) does show SELECT permission for the user (Contoso1\exchadmin) which has NOT YET been mapped to???

Q2

use msdb
go
EXECUTE AS USER = 'Contoso1\exchadmin' 
select Name, physical_name, state_desc, (size)*8/1024, growth from msdb.sys.database_files ORDER BY Name
select Name, physical_name, state_desc, (size)*8/1024, growth from Resources.sys.database_files ORDER BY Name
SELECT * FROM fn_my_permissions('msdb.sys.database_files', 'object')
SELECT * FROM fn_my_permissions('Resources.sys.database_files', 'object')
Revert


Thank you in advance,
Michael

SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
148 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Erland Sommarskog 119.3K Reputation points MVP
    2025-03-04T22:32:59.2466667+00:00

    I guess this is because names in the sys schema resolves to the resource database. (That is, mssqlsystemresource, not the one you call resource!)

    I tried this:

    CREATE LOGIN Villevalle WITH PASSWORD = 'LÄÖÄF"?=='
    EXECUTE AS LOGIN = 'Villevalle'
    go
    SELECT * FROM sys.fn_my_permissions('somedatabase.sys.tables', 'OBJECT')
    SELECT * FROM sys.fn_my_permissions('somedatabase.dbo.sometable', 'OBJECT')
    go
    REVERT
    DROP LOGIN Villevalle
    

    The first SELECt did indeed return a row with SELECT permission. But the other query produced an error message:

    Msg 916, Level 14, State 2, Line 89 The server principal "Villevalle" is not able to access the database "somedatabase" under the current security context.


  2. Erland Sommarskog 119.3K Reputation points MVP
    2025-03-06T22:10:53.3566667+00:00

    My repro above was a little confusing, because when I anonymised the table and database name, I was inconsistent.

    When I tested a little more I found something which is a little puzzling. Look at this:

    CREATE DATABASE tester
    go
    USE tester
    go
    CREATE TABLE testtbl (a int NOT NULL)
    go
    USE tempdb
    go
    CREATE LOGIN testlogin WITH PASSWORD = 'ThisWeeksWeakPassword?'
    EXECUTE AS LOGIN = 'testlogin'
    go
    PRINT 'sys.tables'
    SELECT * FROM sys.fn_my_permissions('tester.sys.tables', 'OBJECT')
    go
    PRINT 'dbo.testtbl'
    SELECT * FROM sys.fn_my_permissions('tester.dbo.testtbl', 'OBJECT')
    go
    PRINT 'dbo.nosuchtbl'
    SELECT * FROM sys.fn_my_permissions('tester.dbo.nosuchtbl', 'OBJECT')
    go
    REVERT
    DROP LOGIN testlogin
    DROP DATABASE tester
    
    
    

    The first and last SELECT runs without error. It is only if you are querying about an actual object that you get an error.


  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.