查看 SQL Server 数据库中角色的所有安全内容?

发布于 2024-08-16 18:55:03 字数 33 浏览 5 评论 0原文

我们如何显示脚本中任何特定角色中添加的所有安全内容?

How can we show all the securable that is added in any particular role in script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她比我温柔 2024-08-23 18:55:03
SELECT
    OBJECT_NAME(major_id), USER_NAME(grantee_principal_id), permission_name
FROM
    sys.database_permissions p
WHERE
    p.class = 1 AND
    OBJECTPROPERTY(major_id, 'IsMSSHipped') = 0
ORDER BY
    OBJECT_NAME(major_id), USER_NAME(grantee_principal_id), permission_name
SELECT
    OBJECT_NAME(major_id), USER_NAME(grantee_principal_id), permission_name
FROM
    sys.database_permissions p
WHERE
    p.class = 1 AND
    OBJECTPROPERTY(major_id, 'IsMSSHipped') = 0
ORDER BY
    OBJECT_NAME(major_id), USER_NAME(grantee_principal_id), permission_name
水晶透心 2024-08-23 18:55:03

这是我用来进行数据库重构、更新或备份的另一种方法。
它还支持列级权限。
该语句生成 GRANT 语句。但适应起来相当容易。

SELECT (case when state_desc like 'GRANT%' then 'GRANT' else state_desc end)
  + ' ' + database_permissions.permission_name 
  + CASE database_permissions.class_desc
        WHEN 'SCHEMA' THEN ' ON SCHEMA::[' + schema_name(major_id) + ']'
        WHEN 'OBJECT_OR_COLUMN' THEN ' ON ' 
          + isnull('[' + schema_name(objects.schema_id) + '].', '') + '['
          + (CASE WHEN minor_id = 0 THEN object_name(major_id)  + ']' COLLATE Latin1_General_CI_AS_KS_WS
            ELSE (SELECT object_name(object_id) + '] (['+ name + '])'
                  FROM sys.columns 
                  WHERE object_id = database_permissions.major_id 
                  AND column_id = database_permissions.minor_id) end)
        WHEN 'DATABASE_PRINCIPAL' THEN ' ON USER::[' + USER_NAME(major_id) + ']'
        WHEN 'DATABASE' Then ''
        WHEN 'SERVICE_CONTRACT' then ' ON CONTRACT::[' 
          + (select name 
             from sys.service_contracts 
             where service_contract_id = major_id) 
          + ']'
        ELSE ' <<' + database_permissions.class_desc + '>>'
    END
  + ' TO [' + database_principals.name + ']'
  + (case when state_desc = 'GRANT_WITH_GRANT_OPTION' then ' WITH GRANT OPTION' else '' end)
  COLLATE Latin1_General_CI_AS_KS_WS
FROM sys.database_permissions
inner JOIN sys.database_principals
ON database_permissions.grantee_principal_id = database_principals.principal_id
LEFT JOIN sys.objects
ON objects.object_id = database_permissions.major_id
WHERE database_permissions.major_id > 0

Here is another one I'm using to do database refactorings, updates or backups.
It also supports column level permissions.
That statement generates GRANT statements. But it's fairly easy to adapt.

SELECT (case when state_desc like 'GRANT%' then 'GRANT' else state_desc end)
  + ' ' + database_permissions.permission_name 
  + CASE database_permissions.class_desc
        WHEN 'SCHEMA' THEN ' ON SCHEMA::[' + schema_name(major_id) + ']'
        WHEN 'OBJECT_OR_COLUMN' THEN ' ON ' 
          + isnull('[' + schema_name(objects.schema_id) + '].', '') + '['
          + (CASE WHEN minor_id = 0 THEN object_name(major_id)  + ']' COLLATE Latin1_General_CI_AS_KS_WS
            ELSE (SELECT object_name(object_id) + '] (['+ name + '])'
                  FROM sys.columns 
                  WHERE object_id = database_permissions.major_id 
                  AND column_id = database_permissions.minor_id) end)
        WHEN 'DATABASE_PRINCIPAL' THEN ' ON USER::[' + USER_NAME(major_id) + ']'
        WHEN 'DATABASE' Then ''
        WHEN 'SERVICE_CONTRACT' then ' ON CONTRACT::[' 
          + (select name 
             from sys.service_contracts 
             where service_contract_id = major_id) 
          + ']'
        ELSE ' <<' + database_permissions.class_desc + '>>'
    END
  + ' TO [' + database_principals.name + ']'
  + (case when state_desc = 'GRANT_WITH_GRANT_OPTION' then ' WITH GRANT OPTION' else '' end)
  COLLATE Latin1_General_CI_AS_KS_WS
FROM sys.database_permissions
inner JOIN sys.database_principals
ON database_permissions.grantee_principal_id = database_principals.principal_id
LEFT JOIN sys.objects
ON objects.object_id = database_permissions.major_id
WHERE database_permissions.major_id > 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文