共用方式為


建立 WMI 事件警示

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

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

本主題內容

開始之前

限制事項

  • SQL Server Management Studio 提供了一種簡單的圖形方式供您管理整個警示系統,建議您利用這個方式設定警示基礎結構。

  • xp_logevent 產生的事件出現在 master 資料庫中。 因此,除非警示的 @database_name'master' 或 NULL,否則, xp_logevent 不會觸發警示。

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

安全性

權限

依預設,只有 系統管理員 (sysadmin) 固定伺服器角色的成員,才能夠執行 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)