MS SQL Server Concepts and Programming Question:

How To List All User Names in a Database?

Tweet Share WhatsApp

Answer:

If you want to see a list of all user names defined in a database, you can use the system view, sys.database_principals as shown in this tutorial exercise:

-- Login with sa

-- Select a database
USE GlobalGuideLineDatabase;
GO

-- List all user names
SELECT name, sid, type, type_desc
FROM sys.database_principals WHERE type = 'S';
GO
<pre>name sid type type_desc
-------------------- ------------------------ ---- ---------
dbo 0x01 S SQL_USER
guest 0x00 S SQL_USER
INFORMATION_SCHEMA NULL S SQL_USER
sys NULL S SQL_USER
ggl_User 0x5EB8701EAEBAA74F86F... S SQL_USER</pre>
(5 row(s) affected)
As you can see, there are 5 user names defined in "GlobalGuideLineDatabase". 4 are defined by SQL Server during the database creation process. Only the last one "ggl_User" was defined by you in the previous tutorial.

Download MS SQL Server PDF Read All 394 MS SQL Server Questions
Previous QuestionNext Question
How To Create a User Name in a Database?How To Find the Login Name Linked to a Given User Name?