共用方式為


如何:建立資料庫層級稽核

在您可以建立資料庫層級稽核規格之前,必須先建立及設定可用於資料庫稽核的 SQL Server Audit 物件。

完成這項工作需要在 SQL Server Management Studio 中使用查詢編輯器來進行以下程序。下列範例會在 AdventureWorks 資料庫的 Person.Contacts 資料表上建立任何 INSERT 作業的資料庫層級稽核,並將結果傳送到 Windows 應用程式事件記錄檔。

建立資料庫層級稽核

  1. 建立稽核物件,並定義目標。

    /* Create the SQL Server Audit object, and send the results to the 
    Windows Application event log. */
    CREATE SERVER AUDIT Test_SQL_Server_Audit
        TO APPLICATION_LOG
        /* The Queue Delay is set to 1000, meaning one second 
             intervals to write to the target. */
        WITH ( QUEUE_DELAY = 1000,  ON_FAILURE = CONTINUE);
    GO;
    
  2. 建立資料庫稽核規格,並將它對應到稽核物件。

    /* Create the Database Audit Specification object using an Audit event for the Person.Contact Table and the FirstName and LastName columns. */
    USE AdventureWorks
    GO;
    CREATE DATABASE AUDIT SPECIFICATION Test_Database_Audit_Specification
    FOR SERVER AUDIT Test_SQL_Server_Audit
        ADD (INSERT 
               ON Person.Contact
               BY dbo)
        WITH (STATE = ON);
    GO
    
  3. 啟用稽核。

    /* Enable the audit. */
    ALTER SERVER AUDIT Test_SQL_Server_Audit
    WITH (STATE = ON);
    GO