共用方式為


建立 WMI 事件警示

本主題描述使用 SQL Server Management Studio 或 Transact-SQL,當由 SQL Server 2014 的 WMI Provider for Server Events 監視的特定 SQL Server 事件發生時,如何引發 SQL Server Agent 警示。

如需使用 WMI 提供者監視 SQL Server 事件的相關信息,請參閱 伺服器事件概念的 WMI 提供者。 如需接收 WMI 事件警示通知所需許可權的相關信息,請參閱 選取 SQL Server Agent 服務的帳戶。 如需 WQL 的詳細資訊,請參閱 搭配伺服器事件的 WMI 提供者使用 WQL

本主題內容

開始之前

限制與制約

  • SQL Server Management Studio 提供簡單、圖形化的方式來管理整個警示系統,而且是設定警示基礎結構的建議方式。

  • 使用 xp_logevent 產生的事件會發生在 master 資料庫中。 因此,xp_logevent 不會觸發警示,除非警示的 @database_name『master』 或 NULL。

  • 僅支援執行 SQL Server Agent 之電腦上的 WMI 命名空間。

安全

權限

根據預設,只有 系統管理員 固定伺服器角色的成員才能執行 sp_add_alert

使用 SQL Server Management Studio

建立 WMI 事件警報

  1. 物件總管 中,按一下加號以展開您想要建立 WMI 事件警示的伺服器。

  2. 點選加號以展開 SQL Server Agent

  3. 以滑鼠右鍵按兩下 [警示],然後選取 [[新增警示]

  4. 在 [[新增警示] 對話框中,於 [名稱] 方塊中,輸入此警示的名稱。

  5. 選取 [啟用] 核取方塊,以啟用警示執行。 根據預設,[啟用] 已被勾選。

  6. 在 [ 類型] 清單中,選取 [WMI 事件警示]。

  7. [WMI 事件警示定義] 底下的 [ 命名空間 ] 方塊中,指定 WMI 查詢語言 (WQL) 語句的 WMI 命名空間,以識別哪些 WMI 事件將觸發此警示。

  8. 在 [ 查詢 ] 方塊中,指定 WQL 語句,以識別此警示所回應的事件。

  9. 按一下 [確定]

使用 Transact-SQL

建立 WMI 事件警示

  1. 物件總管中,連線到資料庫引擎實例。

  2. 在標準列上,按一下 [新增查詢]

  3. 複製下列範例並將其貼到查詢視窗中,然後按一下 [執行]

    -- creates a WMI event alert that retrieves all event properties for any ALTER_TABLE event that occurs on table AdventureWorks2012.Sales.SalesOrderDetail  
    -- This example assumes that the message 54001 already exists.  
    USE msdb ;  
    GO  
    
    EXEC dbo.sp_add_alert  
        @name = N'Test Alert 2',  
        @message_id = 54001  
        @notification_message = N'Error 54001 has occurred on the Sales.SalesOrderDetail table on the AdventureWorks2012 database. Please see the following information...',  
        @wmi_namespace = '\\.\root\Microsoft\SqlServer\ServerEvents\,  
        @wmi_query = N'SELECT * FROM ALTER_TABLE   
    WHERE DatabaseName = 'AdventureWorks2012' AND SchemaName = 'Sales'   
        AND ObjectType='Table' AND ObjectName = 'SalesOrderDetail'';  
    GO  
    

如需詳細資訊,請參閱 sp_add_alert (Transact-SQL)