Login Fixed Server & Database Roles

  • How to find what are the existing logins in the SQL Server instance and also how to find what fixed server and database roles are to the logins in an sql server instance? I have 35 SQL servers in my environment and need to prepare spreadsheet for login and their fixed server and database roles information. Any help is greatly apprecciated.

    Thanks,

    BK

  • BK

    Use the sp_helplogins stored procedure to get a list of how logins map to users and roles in databases.  The query below will return a list of logins and what server roles they are in.

    select

    p.[name] as LoginName, p.type_desc as LoginType, r.[name] as RoleName

    from sys.server_principals p

    left join sys.server_role_members m

    on p.principal_id = m.member_principal_id

      and p.type in ('U', 'G')

    join sys.server_principals r

    on m.role_principal_id = r.principal_id

    John

  • Here is some SQL that you can run:

    -- For logins

    Setnocount on

    Selectcast( serverproperty ('machinename') as nvarchar(128) )as machinename

    ,coalesce ( cast( serverproperty ('instancename')as nvarchar(128) ) , 'default')as instancename

    ,loginname

    ,createdate

    ,updatedate

    ,dbname

    ,language

    ,denylogin

    ,hasaccess

    ,isntname

    ,isntgroup

    ,isntuser

    , password

    From master.dbo.syslogins

    go

    -- Server Roles

    Selectcast( serverproperty ('machinename') as nvarchar(128) )as machinename

    ,coalesce ( cast( serverproperty ('instancename') as nvarchar(128) ) , 'default')as instancename

    ,spt_values.nameas RoleName

    , sysxlogins.nameas LoginName

    from master.dbo.spt_values spt_values

    joinmaster.dbo.sysxlogins sysxlogins

    on spt_values.number & sysxlogins.xstatus = spt_values.number

    where spt_values.low = 0

    andspt_values.type = 'SRV'

    andsysxlogins.srvid IS NULL

    go

    -- Database Users:

    Set nocount on

    Create table #dbusers

    (dbnamesysname

    ,usernamesysname

    ,loginnamesysname

    )

    Exec master.dbo.sp_MSforeachdb @command1 = 'insert into #dbusers (dbname, username, loginname ) select ''?'' as dbname,sysusers.nameas username,syslogins.name as loginname from [?].dbo.sysusersas sysusers join master.dbo.syslogins as syslogins on syslogins.sid = sysusers.sid where loginname ''sa'''

    Selectcast( serverproperty ('machinename') as nvarchar(128) )as machinename

    ,coalesce ( cast( serverproperty ('instancename')as nvarchar(128) ) , 'default')as instancename

    ,dbname

    ,username

    ,loginname

    From #dbusers

    go

    -- Database user roles

    Set nocount on

    Create table #dbuserroles

    (dbnamesysname

    ,usernamesysname

    ,loginnamesysname

    ,rolenamesysname

    )

    Exec master.dbo.sp_MSforeachdb

    @command1

    = 'insert into #dbuserroles (dbname, username, loginname,rolename )

    Select ''?'' as dbname

    ,sysusers.name as username

    ,syslogins.name as loginname

    ,sysgroups.nameas groupname

    From master.dbo.syslogins as syslogins

    Join[?].dbo.sysusers as sysusers

    on sysusers.sid = syslogins.sid

    Join [?].dbo.sysmembers as usergroups

    on usergroups.memberuid= sysusers.uid

    Join [?].dbo.sysusers as sysgroups

    on sysgroups.uid= usergroups.groupuid

    Wheresysgroups.issqlrole = 1

    Andsyslogins.name ''sa''

    And sysgroups.name ''public''

    '

    Selectcast( serverproperty ('machinename') as nvarchar(128) )as machinename

    ,coalesce ( cast( serverproperty ('instancename')as nvarchar(128) ) , 'default')as instancename

    ,dbname

    ,username

    ,loginname

    ,rolename

    From #dbuserroles

    go

    SQL = Scarcely Qualifies as a Language

  • If you need to run it across 35 servers and considering 3rd party tools- you can look at SQL Farms. You can run your sql script to get login info on all 35 servers in parallel and have one posture of all logins across all servers.

    Some alternatives would be using serial linked server calls or osql.

    Editor's Note: : Dr. Omri Bahat works for SQLFarms, a software vendor of SQL Server tools.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply