Sql-Server

SQL Server 伺服器級別審核上的數據庫的多個篩選器

  • May 27, 2016

我正在嘗試創建伺服器級別審核以跟踪 SQL Server 2014 中的數據庫級別事件。我知道我可以在數據庫級別創建它們,但如果可能的話,我希望在伺服器級別審核所有數據庫。

我需要過濾兩個數據庫的伺服器級別審核,並且只針對不是兩個應用程序使用者但我的過濾器無法正常工作的使用者。要麼我建構過濾器錯誤,要麼無法過濾多個數據庫和多個使用者。我的 Google-Fu 無法找到具有多個過濾器的範例:

任何幫助或想法表示讚賞。

CREATE SERVER AUDIT [PCIAudit]
TO FILE
(   FILEPATH = N'D:\SQLAuditLogs\'
   ,MAXSIZE = 2048 MB
   ,RESERVE_DISK_SPACE = OFF
)
WITH
(   QUEUE_DELAY = 0
   ,ON_FAILURE = CONTINUE
)
WHERE ([database_name] = 'DataBase1' OR [database_name] = 'DataBase2' AND [server_principal_id] <> 277 OR [server_principal_id] <> 278 AND NOT [statement] like 'ALTER INDEX%REBUILD%' AND NOT [statement] like 'ALTER INDEX%REORGANIZE%')
GO

ALTER SERVER AUDIT [PCIAudit]
WITH (STATE = ON);
GO



USE [master]

GO

CREATE SERVER AUDIT SPECIFICATION [DDLAudit]
FOR SERVER AUDIT [PCIAudit]
ADD (SCHEMA_OBJECT_CHANGE_GROUP),
ADD (DATABASE_OBJECT_ACCESS_GROUP)
WITH (STATE = ON)
GO

試試這個,它會稍微修改您的WHERE子句以正確遵循 SQL Server運算符優先規則:

CREATE SERVER AUDIT [PCIAudit]
TO FILE
(   FILEPATH = N'D:\SQLAuditLogs\'
   , MAXSIZE = 2048 MB
   , RESERVE_DISK_SPACE = OFF
)
WITH
(   QUEUE_DELAY = 0
   , ON_FAILURE = CONTINUE
)
WHERE ([database_name] = 'DataBase1' OR [database_name] = 'DataBase2') 
   AND [server_principal_id] <> 277 
   AND [server_principal_id] <> 278 
   AND NOT [statement] LIKE 'ALTER INDEX%REBUILD%' 
   AND NOT [statement] LIKE 'ALTER INDEX%REORGANIZE%';
GO

ALTER SERVER AUDIT [PCIAudit]
WITH (STATE = ON);
GO

請注意圓括號(,其中)([database_name] = 'DataBase1' OR [database_name] = 'DataBase2')- 它們告訴 where 子句允許 DataBase1 或 Database2。正如您所擁有的,where 子句設置為允許 DataBase1 或 Database2+server_principal_id<>277。

引用自:https://dba.stackexchange.com/questions/139670