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 921443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:52:32+00:00 2026-05-15T18:52:32+00:00

I have a database with a lot of users in it. Those users belong

  • 0

I have a database with a lot of users in it. Those users belong to different built-in roles in the DB (eg db_ddladmin).

I want to generate a script that creates those same users with the same role assignments to use in a different database. SQL Management Studio seems to only generate sp_addrolemember calls for user-defined roles, not the build-in ones. Is there any way to make it script all roles?

Perhaps there is any other, better tool for generating database scripts from an existing database (preferably, but not necessarily, free)?

  • 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-15T18:52:33+00:00Added an answer on May 15, 2026 at 6:52 pm

    Information about a database’s users and the roles they are assigned to are made available in system views
    sys.database_principals and sys.database_role_members. Review this data with these queries:

    select * from sys.database_principals
    select * from sys.database_role_members
    

    I’ll assume you have database users and roles configured in database A, and you want them copied over to database B. To create the users in the target database:

    • Run the following query in database A
    • Cut, paste, REVIEW, and run the resulting script in database B

    .

    SELECT 'CREATE USER [' + name + '] for login [' + name + ']'
     from sys.database_principals
     where Type = 'U'
      and name <> 'dbo'
    

    To configure the new users in B with the same roles as they have in A:

    • Run the following query in database A
    • Cut, paste, REVIEW, and run the resulting script in database B

    .

    SELECT 'EXECUTE sp_addrolemember ''' + roles.name + ''', ''' + users.name + ''''
     from sys.database_principals users
      inner join sys.database_role_members link
       on link.member_principal_id = users.principal_id
      inner join sys.database_principals roles
       on roles.principal_id = link.role_principal_id
    

    Always review these scripts. There may be exceptions or special cases going on, and you just don’t want to mess up security.

    If the new database is on a different SQL Server instance, you will have to create the SQL logins first. If you have user-defined roles,
    you will need to recreate them first. (Roles and the permissions assigned to them are very open-ended, and I don’t ever want to be in a situation where I’d need to do that!)

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

Sidebar

Related Questions

I have a database with a lot of old phpBB data that contains posts
I have a database with a lot of old phpBB data that contains posts
A have a database on MS-SQL Server 2008 that is growing a lot, year
I have inherited a database that has a lot of cursors in it, and
In my database I have a lot of users who've misspelled their e-mail address.
We have a large database with a lot of data in it. I found
We have a large database containing a lot of stored procedures. Now we are
I am new to LINQ to SQL, but have done a lot of database
I have been given the task to design a database to store a lot
I have a page with a list of lot of Products from the database.

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.