• Articolo - Query SQL per estrarre informazioni riguardanti utenti e permessi

La seguente Query genera una lista di utenti - tipologia - data creazione ecc

Estrazione Utenti e permessi SQL CAL 1

Query lista DB con utenti e permessi:

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 ''?'' AS DB_Name,

case prin.name when ''dbo'' then prin.name + '' (''+ (select SUSER_SNAME(owner_sid) from master.sys.databases where name =''?'') + '')'' else prin.name end AS UserName,

prin.type_desc AS LoginType,

isnull(USER_NAME(mem.role_principal_id),'''') AS AssociatedRole ,create_date,modify_date

FROM sys.database_principals prin

LEFT OUTER JOIN sys.database_role_members mem ON prin.principal_id=mem.member_principal_id

WHERE prin.sid IS NOT NULL and prin.sid NOT IN (0x00) and

prin.is_fixed_role <> 1 AND prin.name NOT LIKE ''##%'''

SELECT

dbname,username ,logintype ,create_date ,modify_date ,

STUFF(

(

SELECT ',' + CONVERT(VARCHAR(500),associatedrole)

FROM @DB_USers user2

WHERE

user1.DBName=user2.DBName AND user1.UserName=user2.UserName

FOR XML PATH('')

)

,1,1,'') AS Permissions_user

FROM @DB_USers user1

GROUP BY

dbname,username ,logintype ,create_date ,modify_date

ORDER BY DBName,username

Query lista SYSADMIN su una istanza SQL

La seguente Query genera una lista di SOLI AMMINISTRATORI - tipologia e data creazione ecc

Estrazione Utenti e permessi SQL CAL 2

SELECT

p.name AS [Name] ,r.type_desc,r.is_disabled,r.create_date , r.modify_date,r.default_database_name

FROM

sys.server_principals r

INNER JOIN sys.server_role_members m ON r.principal_id = m.role_principal_id

INNER JOIN sys.server_principals p ON

p.principal_id = m.member_principal_id

WHERE r.type = 'R' and r.name = N'sysadmin'
  • Fine Articolo HOME

 

 

Ricerca un articolo nel sito