TysonLPrice
Programmer
SQL 2005
I’m an application programmer not a DBA and I’m working on a program to list all the database roles for a selected user and users in roles. I got this from Google for a user’s DB roles and I think it works:
What I need now is a query to select all the users in a given role. Can one of the gurus do two things?
1. Visually I think the posted query works to give all the DB roles for a user on a given DB. Does it seem correct?
2. Using the same methodology can it be changed to give all the users in a given role?
I’m an application programmer not a DBA and I’m working on a program to list all the database roles for a selected user and users in roles. I got this from Google for a user’s DB roles and I think it works:
Code:
DECLARE @username NVARCHAR(128);
SET @username = 'TPrice';
WITH CTE_Roles (role_principal_id)
AS
(
SELECT role_principal_id
FROM sys.database_role_members
-- WHERE member_principal_id = USER_ID(@username)
UNION ALL
SELECT drm.role_principal_id
FROM sys.database_role_members drm
INNER JOIN CTE_Roles CR
ON drm.member_principal_id = CR.role_principal_id
)
SELECT DISTINCT USER_NAME(role_principal_id) RoleName
FROM CTE_Roles
UNION ALL
SELECT 'public'
ORDER BY RoleName;
What I need now is a query to select all the users in a given role. Can one of the gurus do two things?
1. Visually I think the posted query works to give all the DB roles for a user on a given DB. Does it seem correct?
2. Using the same methodology can it be changed to give all the users in a given role?