Sql-Server-2008-R2

列出特定登錄名具有權限的所有表

  • August 8, 2014

這可能是重複的,但我還沒有找到 100% 滿足我的目的的解決方案。我想要實現的是擁有一個腳本(或 SP),它將列出使用者有權訪問的所有表。任何見解都會有所幫助。PS:我對 TSQL 不是很好。:(

非常感謝。

我使用的腳本將向您顯示指定數據庫名稱的所有對象(SP、表、函式)以及對其擁有權限的所有使用者,但您可以縮小搜尋範圍並準確提取您需要的內容。

DECLARE @EXEC_SCRIPT    NVARCHAR(4000)
DECLARE @DBNAME         NVARCHAR(256) = 'DB name'

SET @EXEC_SCRIPT = 'USE ['+ @DBNAME + '] 
select DB_NAME ()                   as [db_name],
a.class_desc                        as [class_type] ,  
user_name(a.grantee_principal_id)           as [user_name] ,
isnull(OBJECT_NAME(a.major_id),'''')            as [object_name],
isnull      (b.name,'''')               as [column_name] , 
a.permission_name                   as [permission_name] , 
a.state_desc                        as [permission_type] , 
c.type_desc                     as [object_type],
CASE d.type 
               WHEN ''A'' THEN N''ApplicationRole''
               WHEN ''R'' THEN N''Role''
               ELSE N''User'' END  as [security_type],
CASE WHEN a.major_id  < 0 THEN N''Y''
    ELSE N''N'' END                as [system_object],
d.is_fixed_role                     as [is_fixed_role]

from sys.database_permissions a left join sys.columns b 
on a.minor_id = b.column_id and a.major_id = b.object_id
left join sys.objects c on  c.object_id = a.major_id
INNER JOIN sys.database_principals AS d ON d.principal_id = a.grantee_principal_id
where a.class_desc <> ''DATABASE''  and user_name(a.grantee_principal_id) <> ''public''
order by user_name'

EXEC master.dbo.sp_executesql @EXEC_SCRIPT

在我的腦海中(目前沒有 MSSQL 框,抱歉):

EXECUTE AS <database user> ;
SELECT * FROM sys.objects WHERE type = 'u' ;

元數據可見性意味著如果數據庫使用者查詢 sys.objects,那麼他們將只能看到他們有權訪問或擁有的對象。

如果您需要他們對這些對象的實際權限,那麼您需要查詢權限管理視圖,如上面 Kin 的評論中所述。

引用自:https://dba.stackexchange.com/questions/73454