sp_helprotect во всех базах данных, использующих sp_MSforeachdb

Кто-нибудь может преобразовать следующий запрос SQL с помощью sp_MSforeachdb? Я использую SQL Server 2005 SP4. Спасибо!

DECLARE @tmpResults TABLE
(Owner sysname, Object sysname, Grantee sysname, Grantor sysname,  ProtectType nvarchar(10), Action nvarchar(60), Column1 sysname)

DECLARE @DB_Grant TABLE
(DBName sysname, Owner sysname, Object sysname, Grantee sysname, Grantor sysname,  ProtectType nvarchar(10), Action nvarchar(60), Column1 sysname)

USE master
INSERT INTO @tmpResults EXEC sp_helprotect;
INSERT INTO @DB_Grant SELECT DB_Name(), t.* FROM @tmpResults t
DELETE @tmpResults

USE model
INSERT INTO @tmpResults EXEC sp_helprotect;
INSERT INTO @DB_Grant SELECT DB_Name(), t.* FROM @tmpResults t
DELETE @tmpResults

-- Repeat for all remaining DB

SELECT * FROM @DB_Grant
ORDER BY DBName, Grantee, Action

1 ответ

Это должно сделать это:

IF OBJECT_ID('tempdb..#dbGrant') IS NOT NULL
    DROP TABLE #dbGrant
IF OBJECT_ID('tempdb..#tempResults') IS NOT NULL
    DROP TABLE #tempResults

CREATE TABLE #dbGrant
(
[DBName] sysname, 
[Owner] sysname, 
[Object] sysname, 
[Grantee] sysname, 
[Grantor] sysname,  
[ProtectType] nvarchar(10), 
[Action] nvarchar(60), 
[Column] sysname
)

CREATE TABLE #tempResults
(
[Owner] sysname, 
[Object] sysname, 
[Grantee] sysname, 
[Grantor] sysname,  
[ProtectType] nvarchar(10), 
[Action] nvarchar(60), 
[Column1] sysname
)

EXEC sp_MSforeachdb 'IF ''?''  NOT IN (''tempDB'')
BEGIN
        INSERT INTO #tempResults EXEC sp_helprotect;
        INSERT INTO #dbGrant SELECT ''?'',t.* FROM #tempResults t
        TRUNCATE TABLE #tempResults
END'

SELECT * FROM #dbGrant
ORDER BY DBName, Grantee, Action

DROP TABLE #dbGrant,#tempResults
Другие вопросы по тегам