共用方式為


使用錯誤號碼建立警示

本主題描述如何在 SQL Server 2014 中建立MICROSOFT SQL Server Agent 警示,當使用 SQL Server Management Studio 或 Transact-SQL 發生特定數位的錯誤時,就會引發該警示。

本主題內容

開始之前

限制與制約

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

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

安全

權限

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

使用 SQL Server Management Studio

使用錯誤號碼建立警示

  1. 在 [物件總管] 中, 按一下加號展開您想要使用錯誤號碼建立警報的伺服器。

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

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

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

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

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

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

  8. 在 [警示] 底下,會根據引發,按兩下 [錯誤號碼],然後輸入警示的有效錯誤號碼。 或者,按兩下 [嚴重性],然後選取將引發警示的特定嚴重性。

  9. 勾選與 [當訊息包含] 複選框對應的方塊,以將警示限制為特定字元序列,然後輸入 關鍵詞或字元字串來作為消息正文。 字元數上限為 100。

  10. 按一下 [確定]

使用 Transact-SQL

使用錯誤號碼建立警示

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

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

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

    -- adds an alert (Test Alert) that runs the Back up the AdventureWorks2012 Database job when fired   
    -- assumes that the message 55001 and the Back up the AdventureWorks2012 Database job already exist.  
    USE msdb ;  
    GO  
    
    EXEC dbo.sp_add_alert  
        @name = N'Test Alert',  
        @message_id = 55001,   
       @severity = 0,   
       @notification_message = N'Error 55001 has occurred. The database will be backed up...',   
       @job_name = N'Back up the AdventureWorks2012 Database' ;  
    GO  
    

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