TSQL 了解数据库角色成员

发布于 2024-08-19 13:14:51 字数 159 浏览 4 评论 0原文

我正在使用 SQL 2000 和 SQL 2005。

我想知道哪些登录对哪些数据库拥有 db_owner 或 db_accessadmin 权限。

我可以单击每个数据库中的用户或数据库角色来查看。 可以使用 TSQL 以更简单的方式完成此操作吗?

提前致谢

I am using SQL 2000 and SQL 2005.

I want to know which logins have db_owner or db_accessadmin rights to which databases.

I can click on users or database roles in every database to see that.
Could this be done in an easier way using TSQL?

Thanks in advance

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

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

发布评论

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

评论(2

想念有你 2024-08-26 13:14:51

适用于 SQL 2000,并且仍然适用于 SQL 2005

SELECT
    USER_NAME(memberuid), USER_NAME(groupuid)
FROM
    sys.sysmembers
WHERE
    USER_NAME(groupuid) IN ('db_owner', 'db_accessadmin')

For SQL 2000 and still works for SQL 2005 too

SELECT
    USER_NAME(memberuid), USER_NAME(groupuid)
FROM
    sys.sysmembers
WHERE
    USER_NAME(groupuid) IN ('db_owner', 'db_accessadmin')
晨曦慕雪 2024-08-26 13:14:51

它很草率,可能有更好的方法,但如果这是一次性的事情,这应该可以完成:

DECLARE
    @db_name SYSNAME,
    @sql VARCHAR(1000)

DECLARE db_cursor CURSOR FOR SELECT Name FROM sys.databases
OPEN db_cursor

FETCH NEXT FROM db_cursor INTO @db_name

WHILE (@@FETCH_STATUS = 0)
BEGIN
    SET @sql =
        'SELECT
            ''' + @db_name + ''' AS [Database],
            USER_NAME(role_principal_id) AS [Role],
            USER_NAME(member_principal_id) AS [User]
        FROM
            ' + @db_name + '.sys.database_role_members
        WHERE
            USER_NAME(role_principal_id) IN (''db_owner'', ''db_accessadmin'')'
    EXEC(@sql)

    FETCH NEXT FROM db_cursor INTO @db_name
END

CLOSE db_cursor
DEALLOCATE db_cursor

SQL 2000 版本应该是:

DECLARE
    @db_name SYSNAME,
    @sql VARCHAR(1000)

DECLARE db_cursor CURSOR FOR SELECT Name FROM master..sysdatabases
OPEN db_cursor

FETCH NEXT FROM db_cursor INTO @db_name

WHILE (@@FETCH_STATUS = 0)
BEGIN
    SET @sql =
        'SELECT
            ''' + @db_name + ''' AS [Database],
            USER_NAME(memberuid) AS [Role],
            USER_NAME(groupuid) AS [User]
        FROM
            sysmembers
        WHERE
            USER_NAME(groupuid) IN (''db_owner'', ''db_accessadmin'')'
    EXEC(@sql)

    FETCH NEXT FROM db_cursor INTO @db_name
END

CLOSE db_cursor
DEALLOCATE db_cursor

It's sloppy and there is probably a better way, but this should get it done if this is a one-time thing:

DECLARE
    @db_name SYSNAME,
    @sql VARCHAR(1000)

DECLARE db_cursor CURSOR FOR SELECT Name FROM sys.databases
OPEN db_cursor

FETCH NEXT FROM db_cursor INTO @db_name

WHILE (@@FETCH_STATUS = 0)
BEGIN
    SET @sql =
        'SELECT
            ''' + @db_name + ''' AS [Database],
            USER_NAME(role_principal_id) AS [Role],
            USER_NAME(member_principal_id) AS [User]
        FROM
            ' + @db_name + '.sys.database_role_members
        WHERE
            USER_NAME(role_principal_id) IN (''db_owner'', ''db_accessadmin'')'
    EXEC(@sql)

    FETCH NEXT FROM db_cursor INTO @db_name
END

CLOSE db_cursor
DEALLOCATE db_cursor

The SQL 2000 version should be:

DECLARE
    @db_name SYSNAME,
    @sql VARCHAR(1000)

DECLARE db_cursor CURSOR FOR SELECT Name FROM master..sysdatabases
OPEN db_cursor

FETCH NEXT FROM db_cursor INTO @db_name

WHILE (@@FETCH_STATUS = 0)
BEGIN
    SET @sql =
        'SELECT
            ''' + @db_name + ''' AS [Database],
            USER_NAME(memberuid) AS [Role],
            USER_NAME(groupuid) AS [User]
        FROM
            sysmembers
        WHERE
            USER_NAME(groupuid) IN (''db_owner'', ''db_accessadmin'')'
    EXEC(@sql)

    FETCH NEXT FROM db_cursor INTO @db_name
END

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