SDU Tools: Script User Defined Database Roles in SQL Server

As part of our free SDU Tools for developers and DBAs, we have many scripting functions. SQL Server allows you to create your own database roles, and generally, you should do this instead of using the fixed database roles like db_owner, or db_datareader and db_datawriter, as it lets you allocate just the required permissions. To allow scripting these out, we've added a tool called ScriptUserDefinedDatabaseRoles.

It's a stored procedure (as it has to change databases) and takes a single parameter: @DatabaseName (sysname) as the name of the database whose roles you want to script.

You can use our tools as a set or as a great example of how to write functions like these.

Find out more

You can see it in action in the main image above, and in the video here:

Access to SDU Tools is one of the benefits of being an SDU Insider, along with access to our other free tools and eBooks. Please just visit here for more info:


2 thoughts on “SDU Tools: Script User Defined Database Roles in SQL Server”

  1. Does anything bad happen if you delete the built in roles? Eg if db_datareader is considered too permissive, I don't want someone to unknowingly use it, I would prefer to delete it but if I delete it does it create weird bugs?

    1. Hi Daniel,

      I've never tried to delete the built-in ones but I'm guessing it wouldn't let you delete any of the built-in ones.



Leave a Reply

Your email address will not be published. Required fields are marked *