SQL Server:查询服务器中所有数据库的数据库用户角色

发布于 2024-09-05 10:03:55 字数 623 浏览 1 评论 0原文

我想查询我的 sql server 实例中所有数据库的数据库用户角色。我修改了 sp_helpuser 的查询:

select  u.name
        ,case when (r.principal_id is null) then 'public' else r.name end
        ,l.default_database_name
        ,u.default_schema_name
        ,u.principal_id
from sys.database_principals u
    left join (sys.database_role_members m join sys.database_principals r on m.role_principal_id = r.principal_id) 
        on m.member_principal_id = u.principal_id
    left join sys.server_principals l on u.sid = l.sid
    where u.type <> 'R'

如何修改它以从所有数据库进行查询? sys.databases 和 sys.database_principals 之间有什么联系?

I would like to make a query for database user roles for all databases in my sql server instance. I modified a query from sp_helpuser:

select  u.name
        ,case when (r.principal_id is null) then 'public' else r.name end
        ,l.default_database_name
        ,u.default_schema_name
        ,u.principal_id
from sys.database_principals u
    left join (sys.database_role_members m join sys.database_principals r on m.role_principal_id = r.principal_id) 
        on m.member_principal_id = u.principal_id
    left join sys.server_principals l on u.sid = l.sid
    where u.type <> 'R'

How can I modify this to query from all databases? What is the link between sys.databases and sys.database_principals?

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

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

发布评论

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

评论(1

为你鎻心 2024-09-12 10:03:55

您可以使用存储过程sp_msforeachdb

sp_msforeachdb:这是一个非常有用的
系统存储过程将
执行您传递给 for 的任何 SQL 脚本
在 SQL 上的每个数据库中
服务器实例。存储过程
只是循环遍历数据库,
写起来很简单,但是可以节省
您不必自己做。

添加一列作为数据库名称,并为数据库名称添加一个 [?] 占位符,然后在 sp_msforeachdb 存储过程中执行脚本,如下所示:

EXECUTE sp_msforeachdb 'select  ''[?]'' as DatabaseName,
             u.name
            ,case when (r.principal_id is null) then ''public'' else r.name end
            ,l.default_database_name
            ,u.default_schema_name
            ,u.principal_id
    from [?].sys.database_principals u
        left join ([?].sys.database_role_members m join [?].sys.database_principals r on m.role_principal_id = r.principal_id) 
            on m.member_principal_id = u.principal_id
        left join [?].sys.server_principals l on u.sid = l.sid
        where u.type <> ''R'''

要将所有这些都放在一个表中,您必须创建一个数据库中的一张表,我们将使用 master 作为示例。

master 数据库中创建表

Create Table master.dbo.userPermissionResults
(
....
)

然后只需将插入语句添加到查询的开头

EXECUTE sp_msforeachdb 'Insert Into master.dbo.userPermissionResults select  ''[?]'' as DatabaseName,
             u.name
            ,case when (r.principal_id is null) then ''public'' else r.name end
            ,l.default_database_name
            ,u.default_schema_name
            ,u.principal_id
    from [?].sys.database_principals u
        left join ([?].sys.database_role_members m join [?].sys.database_principals r on m.role_principal_id = r.principal_id) 
            on m.member_principal_id = u.principal_id
        left join [?].sys.server_principals l on u.sid = l.sid
        where u.type <> ''R'''

您必须为 Insert 语句指定数据库名称,否则它将尝试插入数据存入当前数据库。

You could use the stored procedure sp_msforeachdb

sp_msforeachdb: This is a very useful
system stored procedure that will
execute any SQL script you pass to for
in each of the databases on your SQL
Server instance. The stored procedure
just loops through the databases,
which is simple to write, but it saves
you from having to do it yourself.

Add a column for the database name and a [?] placeholder for the the database name and then execute the script within the sp_msforeachdb stored proc like this:

EXECUTE sp_msforeachdb 'select  ''[?]'' as DatabaseName,
             u.name
            ,case when (r.principal_id is null) then ''public'' else r.name end
            ,l.default_database_name
            ,u.default_schema_name
            ,u.principal_id
    from [?].sys.database_principals u
        left join ([?].sys.database_role_members m join [?].sys.database_principals r on m.role_principal_id = r.principal_id) 
            on m.member_principal_id = u.principal_id
        left join [?].sys.server_principals l on u.sid = l.sid
        where u.type <> ''R'''

To get this all in one table you would have to create a table in one database, we'll use the master as an example.

Create the table in the master database

Create Table master.dbo.userPermissionResults
(
....
)

Then simply add the insert statement to the start of the query

EXECUTE sp_msforeachdb 'Insert Into master.dbo.userPermissionResults select  ''[?]'' as DatabaseName,
             u.name
            ,case when (r.principal_id is null) then ''public'' else r.name end
            ,l.default_database_name
            ,u.default_schema_name
            ,u.principal_id
    from [?].sys.database_principals u
        left join ([?].sys.database_role_members m join [?].sys.database_principals r on m.role_principal_id = r.principal_id) 
            on m.member_principal_id = u.principal_id
        left join [?].sys.server_principals l on u.sid = l.sid
        where u.type <> ''R'''

You must specify the database name for the Insert statement otherwise it will try to insert the data into the current database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文