Es descuidado y es probable que haya una mejor manera, pero esto debe lograr que se haga si esto es una cosa de una sola vez:
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
La versión SQL 2000 debe ser:
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
Muchas gracias. Y para ejecutarlo en toda la base de datos que hice: EXEC SP_MSFOREACHDB 'USE [?]; SELECT \t \t \t \t DB_NAME(), USER_NAME (memberUid), USER_NAME (groupuid) \t \t \t \t DE \t \t \t \t sysmembers \t \t \t \t DONDE \t \t \t \t USER_NAME (groupuid) IN ('' db_owner '', '' db_accessadmin '') \t \t \t \t y user_name (memberuid) no como ''% dbo% '' ' – Manjot
lo siento, lo hice en mi SQL Server local, ¡pero olvidé de publicarlo! – gbn