查找对数据库中表的用户帐户访问权限

发布于 2025-01-04 05:17:52 字数 71 浏览 3 评论 0原文

我正在尝试编写一个查询来告诉我某个用户可以访问数据库中的哪些表。这是域用户而不仅仅是 SQL 用户。

提前致谢。

I'm trying to write a query to tell me what tables in a database a certain user can access. This is a domain user and not just a SQL user.

Thanks in advance.

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

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

发布评论

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

评论(1

零度℉ 2025-01-11 05:17:52

假设您只对用户/登录而不是角色等感兴趣,您可以按如下方式获取信息:

WITH    RootPermissions
          AS ( SELECT   USER_NAME(p.grantee_principal_id) AS UserName ,
                        dp.principal_id ,
                        dp.type_desc AS UserType ,
                        OBJECT_NAME(p.major_id) AS ObjectName ,
                        p.class_desc AS ObjectType ,
                        p.permission_name AS Permission,
                        p.state_desc AS PermissionState
               FROM     sys.database_permissions AS p
                        INNER JOIN sys.database_principals AS dp ON p.grantee_principal_id = dp.principal_id
             ),
        UnionResults ( UserName, UserType, ObjectName, ObjectType, Permission, PermissionState, role_name )
          AS ( SELECT   UserName ,
                        UserType ,
                        ObjectName ,
                        ObjectType ,
                        Permission ,
                        PermissionState ,
                        CAST(NULL AS SYSNAME) AS role_name
               FROM     RootPermissions AS p
               WHERE    ( UserType <> 'DATABASE_ROLE' )
               UNION
               SELECT   rm.member_principal_name ,
                        rm.principal_type_desc ,
                        p.ObjectType ,
                        p.ObjectName ,
                        p.Permission ,
                        p.PermissionState ,
                        rm.role_name
               FROM     RootPermissions AS p
                        RIGHT OUTER JOIN ( SELECT   rm.role_principal_id ,
                                                    dp.type_desc AS principal_type_desc ,
                                                    rm.member_principal_id ,
                                                    USER_NAME(rm.member_principal_id) AS member_principal_name ,
                                                    USER_NAME(rm.role_principal_id) AS role_name
                                           FROM     sys.database_role_members AS rm
                                                    INNER JOIN sys.database_principals AS dp ON rm.member_principal_id = dp.principal_id
                                         ) AS rm ON rm.role_principal_id = p.principal_id
             )
    SELECT  ObjectName ,
            UserName ,
            ObjectType ,
            UserType ,
            Permission ,
            PermissionState ,
            role_name
    FROM    UnionResults
    WHERE   ( ObjectName IS NOT NULL )
    ORDER BY ObjectName ,
            UserName

Assuming your only interested in users/logins as opposed to roles etc, you grab the information your after as follows:

WITH    RootPermissions
          AS ( SELECT   USER_NAME(p.grantee_principal_id) AS UserName ,
                        dp.principal_id ,
                        dp.type_desc AS UserType ,
                        OBJECT_NAME(p.major_id) AS ObjectName ,
                        p.class_desc AS ObjectType ,
                        p.permission_name AS Permission,
                        p.state_desc AS PermissionState
               FROM     sys.database_permissions AS p
                        INNER JOIN sys.database_principals AS dp ON p.grantee_principal_id = dp.principal_id
             ),
        UnionResults ( UserName, UserType, ObjectName, ObjectType, Permission, PermissionState, role_name )
          AS ( SELECT   UserName ,
                        UserType ,
                        ObjectName ,
                        ObjectType ,
                        Permission ,
                        PermissionState ,
                        CAST(NULL AS SYSNAME) AS role_name
               FROM     RootPermissions AS p
               WHERE    ( UserType <> 'DATABASE_ROLE' )
               UNION
               SELECT   rm.member_principal_name ,
                        rm.principal_type_desc ,
                        p.ObjectType ,
                        p.ObjectName ,
                        p.Permission ,
                        p.PermissionState ,
                        rm.role_name
               FROM     RootPermissions AS p
                        RIGHT OUTER JOIN ( SELECT   rm.role_principal_id ,
                                                    dp.type_desc AS principal_type_desc ,
                                                    rm.member_principal_id ,
                                                    USER_NAME(rm.member_principal_id) AS member_principal_name ,
                                                    USER_NAME(rm.role_principal_id) AS role_name
                                           FROM     sys.database_role_members AS rm
                                                    INNER JOIN sys.database_principals AS dp ON rm.member_principal_id = dp.principal_id
                                         ) AS rm ON rm.role_principal_id = p.principal_id
             )
    SELECT  ObjectName ,
            UserName ,
            ObjectType ,
            UserType ,
            Permission ,
            PermissionState ,
            role_name
    FROM    UnionResults
    WHERE   ( ObjectName IS NOT NULL )
    ORDER BY ObjectName ,
            UserName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文