共用方式為


使用嚴重性層級建立警示

適用於:SQL ServerAzure SQL 受控實例

重要

Azure SQL 受控實例上,目前支援大部分但並非所有 SQL Server Agent 功能。 如需詳細資訊,請參閱 與 SQL Server 的 Azure SQL 受控實例 T-SQL 差異。

本主題描述如何使用 SQL Server Management Studio 或 Transact-SQL,在 SQL Server 中發生特定嚴重性層級的事件時,建立 Microsoft SQL Server Agent 的警示。

開始之前

限制與限制條件

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

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

  • 從 19 到 25 的嚴重性層級會將 SQL Server 訊息傳送至Microsoft Windows 應用程式記錄檔,並觸發警示。 嚴重性層級小於 19 的事件只有在您已使用 sp_altermessage、RAISERROR WITH LOG 或 xp_logevent 強制將它們寫入 Windows 應用程式記錄檔時,才會觸發警示。

安全

權限

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

使用 SQL Server Management Studio

使用嚴重性等級建立警示

  1. 在 [物件總管] 中, 按一下加號來展開您想要依嚴重性層級建立警示的伺服器。

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

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

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

  5. 在 [類型] 列表中,選取 [SQL Server 事件警示]

  6. 在 [事件警示定義],在 [資料庫名稱] 清單中,選取資料庫以將警示限制為特定資料庫。

  7. 在 [警示] 底下,會根據引發,點擊 [嚴重性],然後選擇將引發警示的特定嚴重性。

  8. 核取 [當訊息包含] 複選框,以將警示限制為特定字元序列,然後為 訊息文字輸入關鍵詞或字元字串。 字元數上限為 100。

  9. 按下 確定

使用 Transact-SQL

使用嚴重性層級建立警示

  1. 對象總管中,連接到Database Engine的實例。

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

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

    -- Adds an alert (Test Alert) that notifies the
    -- Alert Operator via email when an error with a 
    -- severity of 23 is detected.
    
    -- Assumes that the Alert Operator already exists 
    -- and that database mail is configured.
    
    USE msdb ;  
    GO  
    
    EXEC dbo.sp_add_alert @name=N'Test Alert', 
      @message_id = 0, 
      @severity = 23, 
      @enabled = 1, 
      @include_event_description_in = 1
    ;
    GO
    
    EXEC dbo.sp_add_notification @alert_name=N'Test Alert',
      @operator_name=N'Alert Operator',
      @notification_method=1
    ;
    GO
    
    

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