データベース監査の仕様の更新
適宜実行する用途のスクリプト
監査名とサーバー監査は無効化しなくても大丈夫
code:sql
-- データベース監査の仕様の有効化
ALTER DATABASE AUDIT SPECIFICATION データベース監査仕様 WITH (STATE = OFF) go
-- 全テーブル、全プリンシパルに対して設定する
SET NOCOUNT ON
DECLARE @obj_name varchar(50)
DECLARE @sch_name varchar(50)
DECLARE @principle_name varchar(50)
DECLARE @cmd_text nvarchar(4000)
DECLARE pri_csr CURSOR FOR
SELECT name FROM sys.database_principals WHERE type in ('U', 'S')
OPEN pri_csr
FETCH NEXT FROM pri_csr INTO @principle_name
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE obj_csr CURSOR FOR
SELECT a.name, b.name FROM sys.objects a, sys.schemas b WHERE a.schema_id = b.schema_id and a.type in ('U', 'P')
OPEN obj_csr
FETCH NEXT FROM obj_csr INTO @obj_name, @sch_name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmd_text =
ADD ( SELECT ON + @sch_name + '.+ @obj_name + ' BY + @principle_name + ' ),
ADD ( UPDATE ON + @sch_name + '.+ @obj_name + ' BY + @principle_name + ' ),
ADD ( INSERT ON + @sch_name + '.+ @obj_name + ' BY + @principle_name + ' ),
ADD ( DELETE ON + @sch_name + '.+ @obj_name + ' BY + @principle_name + ' ),
ADD ( EXECUTE ON + @sch_name + '.+ @obj_name + ' BY + @principle_name + ' ) '
--PRINT @cmd_text
EXECUTE sp_executesql @cmd_text
FETCH NEXT FROM obj_csr INTO @obj_name, @sch_name
END
CLOSE obj_csr
DEALLOCATE obj_csr
FETCH NEXT FROM pri_csr INTO @principle_name
END
CLOSE pri_csr
DEALLOCATE pri_csr
SET NOCOUNT OFF
-- データベース監査の仕様の有効化
ALTER DATABASE AUDIT SPECIFICATION データベース監査仕様 WITH (STATE = ON) go