Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 851503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:26:15+00:00 2026-05-15T07:26:15+00:00

I would like to make a query for database user roles for all databases

  • 0

I would like to make a query for database user roles for all databases in my sql server instance. I modified a query from sp_helpuser:

select  u.name
        ,case when (r.principal_id is null) then 'public' else r.name end
        ,l.default_database_name
        ,u.default_schema_name
        ,u.principal_id
from sys.database_principals u
    left join (sys.database_role_members m join sys.database_principals r on m.role_principal_id = r.principal_id) 
        on m.member_principal_id = u.principal_id
    left join sys.server_principals l on u.sid = l.sid
    where u.type <> 'R'

How can I modify this to query from all databases? What is the link between sys.databases and sys.database_principals?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:26:16+00:00Added an answer on May 15, 2026 at 7:26 am

    You could use the stored procedure sp_msforeachdb

    sp_msforeachdb: This is a very useful
    system stored procedure that will
    execute any SQL script you pass to for
    in each of the databases on your SQL
    Server instance. The stored procedure
    just loops through the databases,
    which is simple to write, but it saves
    you from having to do it yourself.

    Add a column for the database name and a [?] placeholder for the the database name and then execute the script within the sp_msforeachdb stored proc like this:

    EXECUTE sp_msforeachdb 'select  ''[?]'' as DatabaseName,
                 u.name
                ,case when (r.principal_id is null) then ''public'' else r.name end
                ,l.default_database_name
                ,u.default_schema_name
                ,u.principal_id
        from [?].sys.database_principals u
            left join ([?].sys.database_role_members m join [?].sys.database_principals r on m.role_principal_id = r.principal_id) 
                on m.member_principal_id = u.principal_id
            left join [?].sys.server_principals l on u.sid = l.sid
            where u.type <> ''R'''
    

    To get this all in one table you would have to create a table in one database, we’ll use the master as an example.

    Create the table in the master database

    Create Table master.dbo.userPermissionResults
    (
    ....
    )
    

    Then simply add the insert statement to the start of the query

    EXECUTE sp_msforeachdb 'Insert Into master.dbo.userPermissionResults select  ''[?]'' as DatabaseName,
                 u.name
                ,case when (r.principal_id is null) then ''public'' else r.name end
                ,l.default_database_name
                ,u.default_schema_name
                ,u.principal_id
        from [?].sys.database_principals u
            left join ([?].sys.database_role_members m join [?].sys.database_principals r on m.role_principal_id = r.principal_id) 
                on m.member_principal_id = u.principal_id
            left join [?].sys.server_principals l on u.sid = l.sid
            where u.type <> ''R'''
    

    You must specify the database name for the Insert statement otherwise it will try to insert the data into the current database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 422k
  • Answers 422k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can setup any rack endpoint as a route in… May 15, 2026 at 11:26 am
  • Editorial Team
    Editorial Team added an answer You're looking for $.each(array, function(i, element) { ... }) That… May 15, 2026 at 11:26 am
  • Editorial Team
    Editorial Team added an answer I'm not sure exactly what you are asking for, but… May 15, 2026 at 11:26 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.