2008R2: What's the T-SQL to show users and roles?

Hi All

I have the below to select a specific user, and the role column is NULL.
SELECT TOP 100 * 
FROM sys.sysusers 
WHERE name='my_domain\my_user'

Open in new window

I created the user, role, and assigment with the below T-SQL:
-- Add role
CREATE ROLE [role_name] AUTHORIZATION [dbo]

-- Add User
CREATE USER [my_domain\my_user] FOR LOGIN [my_domain\my_user] WITH DEFAULT_SCHEMA=[dbo]

-- Assign role to user
EXEC sp_addrolemember 'role_name', 'my_domain\my_user'

Open in new window

Question:  What's the T-SQL to show users and roles?

Thanks.
Jim
LVL 67
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

virtuadeptCommented:
select rp.name as database_role, mp.name as database_user
from sys.database_role_members drm
join sys.database_principals rp on (drm.role_principal_id = rp.principal_id)
join sys.database_principals mp on (drm.member_principal_id = mp.principal_id)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
Bingo bango.

As an aside, the problem I was attempting to troubleshoot by asking this question was 'Why am I getting an execute privs not granted on some_object_name' error was resolved by my having to go into the reprots portal and re-map the .rdl to the data source.

I'll add this code to my grab-bag of handy T-SQL.

Thanks again.
Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.