Situatie
Solutie
create procedure BD_FindDbOwner_prc
as
DECLARE @baza VARCHAR(50)
DECLARE @sql NVARCHAR(MAX)
CREATE TABLE #UseriDbo
(
ServerName SYSNAME,
DbName SYSNAME,
UserName SYSNAME,
TypeOfLogIn VARCHAR(50),
PermissionLevel VARCHAR(50)
)
DECLARE xcursor CURSOR
LOCAL FAST_FORWARD
FOR
SELECT name
FROM master.sys.databases
where state_desc=’online’
OPEN xcursor
FETCH NEXT FROM xcursor INTO @baza
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql = ‘use ‘+@baza +’;’+ ‘SELECT
ServerName=@@servername, dbname=db_name(db_id()),p.name as UserName, p.type_desc as TypeOfLogin,
pp.name as PermissionLevel
FROM sys.database_role_members roles
JOIN sys.database_principals p ON roles.member_principal_id = p.principal_id
JOIN sys.database_principals pp ON roles.role_principal_id = pp.principal_id
where pp.name=”db_owner” and p.name<>”dbo”’
INSERT INTO #UseriDbo
EXEC sp_executesql @sql
FETCH NEXT FROM xcursor INTO @baza
END
CLOSE xcursor
DEALLOCATE xcursor
SELECT * FROM #UseriDbo
DROP TABLE #UseriDbo
Leave A Comment?