Skip to main content

Run this script to list users in each database along with the roles assigned to each user as well as login mapped to each user and the status of that login.
The Database Role column returns NULL when the user has no role assigned and the Instance Login column returns NULL if the user is an orphaned user.

-- To get database users and their roles for each user database --
-- lines added to include ALL users even those without roles --
-- lines added to add instance login names and status --

create table #userlist (
    [Server Name] varchar(20)
    ,[Database Name] varchar(20)
    ,[Database User] varchar(50)
    ,[Database Role] varchar(50)
    ,[Instance Login] varchar(50)
    ,Status varchar(15)
insert into #userlist
exec sp_MSforeachdb @command1 = '
USE [?]
IF      ''?''     NOT IN ("tempdb","model","msdb","master")
select @@servername as instance_name
, ''?'' as database_name
, as database_user
, as database_role
, as instance_login
when sp.is_disabled = 1 then ''Disabled''
when sp.is_disabled = 0 then ''Enabled''
from sys.database_principals rp 
left outer join sys.database_role_members drm on (drm.member_principal_id = rp.principal_id) 
left outer join sys.database_principals mp on (drm.role_principal_id = mp.principal_id)
left outer join sys.server_principals sp on (rp.sid=sp.sid)
where rp.type_desc in (''WINDOWS_USER'',''SQL_USER'')
select * from #userlist;
drop table #userlist;