Scripting SQL Server permissions

前端 未结 9 1960
轮回少年
轮回少年 2021-01-30 02:58

I want to copy all the permission I\'ve set on stored procedures and other stuff from my development database to my production database. It\'s incredibly cumbersome, not to ment

9条回答
  •  情话喂你
    2021-01-30 03:38

    Yes, you can use a script like this to generate another script

    SET NOCOUNT ON;
    DECLARE @NewRole varchar(100), @SourceRole varchar(100);
    
    -- Change as needed
    SELECT @SourceRole = 'Giver', @NewRole = 'Taker';
    
    SELECT
        state_desc + ' ' + permission_name + ' ON ' + OBJECT_NAME(major_id) + ' TO ' + @NewRole
    FROM
        sys.database_permissions
    WHERE
        grantee_principal_id = DATABASE_PRINCIPAL_ID(@SourceRole) AND
        -- 0 = DB,  1 = object/column, 3 = schema. 1 is normally enough
        class <= 3
    

    This is taken from my answer here

提交回复
热议问题