T-SQL to list all the user mappings with database roles/permissions for a Login

前端 未结 7 2470
无人及你
无人及你 2020-12-06 05:09

I am looking for a t-sql script which can list the databases and and the respective roles/privileges mapped for a particular user. Using SQL Server 2008 R2.

7条回答
  •  無奈伤痛
    2020-12-06 05:56

    Is this the kind of thing you want? You might want to extend it to get more info out of the sys tables.

    use master
    
    DECLARE @name VARCHAR(50) -- database name 
    
    DECLARE db_cursor CURSOR FOR 
    select name from sys.databases
    
    OPEN db_cursor  
    FETCH NEXT FROM db_cursor INTO @name  
    
    WHILE @@FETCH_STATUS = 0  
    BEGIN  
           print @name
          exec('USE ' +  @name + '; select rp.name, mp.name from sys.database_role_members drm
                join sys.database_principals rp on (drm.role_principal_id = rp.principal_id)
                join sys.database_principals mp on (drm.member_principal_id = mp.principal_id)')
           FETCH NEXT FROM db_cursor INTO @name  
    END  
    
    CLOSE db_cursor  
    DEALLOCATE db_cursor
    

提交回复
热议问题