How to script SQL server database role?

前端 未结 8 1138
遇见更好的自我
遇见更好的自我 2021-02-01 17:49

I need to make a script to copy one particular database role from one SQL server to another.

Is there an easy way to generate a script that creates the role and all the

8条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 18:26

    IN SSMS right clicking user/login/role node and selecting 'Script As' will script this particular user / login / role. You can't script role mebership this way though.

    Visual Studio with 'Database Drvelopment' option and Red Gate SQL Compare can generate the change script between to databases, this includes users, roles and role membership.

    Generated by VS role membership looks like this:

    EXECUTE sp_addrolemember @rolename = N'db_datareader', @membername = N'DOMAIN\User';
    

    If you don't have VS, you can either write those manually, or create a sql script for generating them.

    I'm sure there also should be a free tool to do something like this, but since I don't need it as I have Visual Studio, I never looked for it.

    Edit: I just realized that I'm answering a wrong question, you are asking about role permission and I'm telling you about role membership. Sorry about this. I'll leave this answer here in case it can be useful to someone else. Answer by Alex Aza looks good.

提交回复
热议问题