How can I query the list of database roles in a SQL Server 2000 database?
Asked Answered
M

2

24

In Sql Server 2000, is it possible to return, via SQL query, a complete list of database roles that exist in a given database?

I know it is possible to see these roles by expanding the Security, Roles, and Database Roles nodes in SQL Server Management Studio, but I'd like to get them through a query that I can parse programmatically.

Screenshot of the nodes in question

To clarify, I'm not looking for a list of users with their roles, but just the list of roles themselves.

Monoxide answered 3/1, 2013 at 16:36 Comment(0)
B
35

Every database in SQL Server 2000 has a sysusers system table

Probably something like

Use <MyDatabase>

Select 
  [name]
From
  sysusers
Where
  issqlrole = 1

will do the trick

Boise answered 3/1, 2013 at 16:51 Comment(4)
I had not realized that sysusers included roles as well as users. I checked your code and it indeed returns the correct data. Many thanks!Monoxide
what a funny place to put it.Orton
NB sys.sysusers is deprecated. From 2008 should be using sys.database_principles.Frock
^ Should be sys.database_principals?Autolysin
M
12

With our SQL Server 2016 this works for me

Use Sandbox
Select
  name, principal_id
From
  sys.database_principals 
Where
  type = 'R' and principal_id < 16384

where Sandbox is the name of my database. (I'm using SQL with ESRI ArcGIS Enterprise 10.6.)

Marsupium answered 15/6, 2018 at 21:44 Comment(1)
Should type = 'R' and principal < 16384 not be type = 'R' and principal_id < 16384?Heated

© 2022 - 2024 — McMap. All rights reserved.