Monday, September 9, 2024

How can I retrieve a list of all users in a specific SQL Server database?

 You can get the list of all users in a database using this query:

SELECT name AS Username FROM sys.database_principals WHERE type IN ('S', 'U') -- 'S' for SQL user, 'U' for Windows user AND name NOT LIKE '##%'; -- Exclude system users

This lists all users in the current database.