sql server - How can i get the list of all database name with their username and roles in t-sql? -


i'm trying list of database name username , roles. this: image

here's code can dbname:

    select      left(ltrim(rtrim(@@servername)), charindex('\', ltrim(rtrim(@@servername))) -1) servername     ,convert(nvarchar,dec.local_net_address)  ipadress     ,suser_sname(owner_sid) 'localadmin'     , db.name dbname sys.dm_exec_connections dec cross join sys.databases  db dec.session_id = @@spid  , suser_sname(owner_sid) <> 'sa' 

you can use this:

declare @db_users table (     dbname sysname     ,username sysname     ,logintype sysname     ,associatedrole varchar(max)     ,create_date datetime     ,modify_date datetime     )  insert @db_users exec sp_msforeachdb ' use [?] select ''?'' db_name, case prin.name when ''dbo'' prin.name + '' (''+ (select suser_sname(owner_sid) master.sys.databases name =''?'') + '')'' else prin.name end username, prin.type_desc logintype, isnull(user_name(mem.role_principal_id),'''') associatedrole ,create_date,modify_date sys.database_principals prin left outer join sys.database_role_members mem on prin.principal_id=mem.member_principal_id prin.sid not null , prin.sid not in (0x00) , prin.is_fixed_role <> 1 , prin.name not ''##%'''  select dbname     ,username     ,logintype     ,create_date     ,modify_date     ,stuff((             select ',' + convert(varchar(500), associatedrole)             @db_users user2             user1.dbname = user2.dbname                 , user1.username = user2.username             xml path('')             ), 1, 1, '') permissions_user @db_users user1 group dbname     ,username     ,logintype     ,create_date     ,modify_date order dbname     ,username 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -