I am going to get the list of all users, including Windows users and 'sa', who have access to a particular database in MS SQL Server.
Basically, I would like the list to look like as what is shown in SQL Server Management Studio
(i.e. the list that is shown when you expand [databse] -> Security -> Users
) with one important exception: I do not want to see the 'dbo'
in the list. Rather, I would like to see the actual user who owns the database. So, for example, if 'sa' is the 'dbo'
, 'sa'
must be included in the list instead of 'dbo'
. Another note not to be missed is, the list in the SQL Server Management Studio
normally shows Windows users in addition to SQL users, And I would like those users to be included as well.
So far, I have been able to come up with the following query:
SELECT * FROM sys.database_principals where (type='S' or type = 'U')
This query is almost right but the problem is it doesn't satisfy the 'dbo'
condition.
How can I change this query or should I use another one?