SQL Server: Find Users in SQL Server

Users 

Question: Is there a query to run in SQL Server that will return all Users created?

Answer: In SQL Server, there is a system view called sys.database_principals. You can run a query against this system view that returns all of the Users that have been created in SQL Server as well as information about these Users.

To retrieve all Users in SQL Server, you can execute the following SQL statement:

SELECT *
FROM master.sys.database_principals;

The sys.database_principals view contains the following columns:

ColumnExplanation
nameThis is the user_name that was assigned in CREATE USER statement
principal_idUnique numeric value
typeType of principal
S = SQL Server user
U = Windows user
G = Windows group
A = Application role
R = Database role
C = Certificate mapped
K = Asymmetric key mapped
type_descDescription for type of principal
SQL_USER
WINDOWS_USER
WINDOWS_GROUP
APPLICATION_ROLE
DATABASE_ROLE
CERTIFICATE_MAPPED_USER
ASSYMETRIC_KEY_MAPPED_USER
default_schema_nameName to use when schema is not specified
create_dateDate/time when User was created using the CREATE USER statement
modify_dateDate/time when User was modified
owning_principal_idID of the principal that owns this user
sidThis is th sid that was assigned using the CREATE LOGIN statement
is_fixed_role0 or 1
authentication_typeNONE, WINDOWS, INSTANCE
default_language_name 
default_language_lcid 

Older Version Compatibility

In older versions of SQL Server, you can retrieve all Users using the SQL Server 2000 system tables such as the sys.sysusers table.

To retrieve all Users in SQL Server using the SQL Server 2000 system tables, you can execute the following SQL statement:

SELECT *
FROM master.sys.sysusers;

The sys.sysusers system table (SQL Server 2000) contains the following columns:

ColumnExplanation
uidUnique numeric value
statusNot used
nameThis is the user_name that was assigned in CREATE USER statement
sidThis is the sid that was assigned in CREATE LOGIN statement
rolesNot used
createdateDate/time when User was created using the CREATE USER statement
updatedateDate/time when User was modified
altuidNot used
passwordNo used
gidGroup ID assigned to the User
environNot used
hasdbaccess0 or 1
islogin0 or 1
isntname0 or 1
isntgroup0 or 1
isntuser0 or 1
issqluser0 or 1
isaliased0 or 1
issqlrole0 or 1
isapprole0 or 1

This Post Has One Comment

Leave a Reply