In SQL Server 2005, is there an easy way to “copy” permissions on an object from one user/role to another?

后端 未结 2 830
无人及你
无人及你 2021-01-15 06:46

I asked another question about roles and permissions, which mostly served to reveal my ignorance. One of the other outcomes was the advice that one should generally stay aw

2条回答
  •  生来不讨喜
    2021-01-15 07:08

    Working from memory (no SQL on my gaming 'pooter), you can use sys.database_permissions

    Run this and paste the results into a new query.

    Edit, Jan 2012. Added OBJECT_SCHEMA_NAME.
    You may need to pimp it to support schemas (dbo.) by joining onto sys.objects

    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_SCHEMA_NAME(major_id) + '.' + 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;
    

提交回复
热议问题