4

I want operators to be notified when I throw a custom error message. My understanding is that I need to add a message to sys.messages, then I can either RAISERROR or THROW that error ID. Creating an alert on the message ID and setting it to send to operators should create those notifications.

I've created a SQL Agent alert on the message ID of 50005, and am using the following T-SQL to create the message:

EXEC sp_addmessage @msgnum = 50005,
          @severity = 16,
          @msgtext = N'%s';
GO

Then, if I execute this:

RAISERROR (50005, -- Message id.
       16, -- Severity,
       1, -- State,
       N'My custom message');
THROW 50005, 'My custom message', 1;
GO

I get the following output as expected from both RAISERROR and THROW:

Msg 50005, Level 16, State 1, Line 68 My custom message

When I view the history of the alert though, it shows that it has not been triggered, and the operator does not receive an email update.

My agent alert looks like this:

Agent Alert Setup

Scripting the alert generates the following:

USE [msdb]
GO
EXEC msdb.dbo.sp_update_alert @name=N'Alert DBA on custom errors', 
    @message_id=50005, 
    @severity=0, 
    @enabled=1, 
    @delay_between_responses=0, 
    @include_event_description_in=1, 
    @database_name=N'', 
    @notification_message=N'', 
    @event_description_keyword=N'', 
    @performance_condition=N'', 
    @wmi_namespace=N'', 
    @wmi_query=N'', 
    @job_id=N'00000000-0000-0000-0000-000000000000'
GO
EXEC msdb.dbo.sp_update_notification @alert_name=N'Alert DBA on custom errors', @operator_name=N'My Operator', @notification_method = 1
GO

For your convenience, if you try to recreate my issue this will help you drop from sys.messages:

sp_dropmessage @msgnum = 50005; 
GO
  • What does your Agent alert definition look like? – Fred Shope Jan 22 '16 at 19:18
  • I've updated the question to include a screenshot and the alert scripted. – Dan Bowling Jan 22 '16 at 19:23
4

Please refer to Create a User-Defined Event

By default, user-defined messages of severity lower than 19 are not sent to the Microsoft Windows application log when they occur. User-defined messages of severity lower than 19 therefore do not trigger SQL Server Agent alerts.

So your message should be

EXEC sp_addmessage @msgnum = 50005,
      @severity = 1,  -- Informational messages that return status information or report errors that are not severe. The Database Engine does not raise system errors with severities of 0 through 9.
      @msgtext = N'%s';
GO

When you use THROW, it defaults to severity 16 which does not fire the alert.

RAISERROR (50005, -- Message id.
       1, -- Severity,
       1, -- State,
       N'My custom message') with log;

USE [msdb]
GO

/****** Object:  Alert [Alert DBA on custom errors]    Script Date: 1/22/2016 3:17:22 PM ******/
EXEC msdb.dbo.sp_add_alert @name=N'Alert DBA on custom errors', 
        @message_id=50005, 
        @severity=0, 
        @enabled=1, 
        @delay_between_responses=0, 
        @include_event_description_in=0, 
        @database_name=N'AdventureWorks2012_test', 
        @category_name=N'[Uncategorized]', 
        @job_id=N'00000000-0000-0000-0000-000000000000'
GO

enter image description here

Also, make sure you use @delay_between_responses to a sensible value so that your inbox is not flooded with emails.

Also, refer to my answer for Monitoring free space in SQL Server data files using user defined message and alerts.

0

Adding "with log" to your raiserror statement will make the alert fire.

RAISERROR (50005, -- Message id.
       16, -- Severity,
       1, -- State,
       N'My custom message') WITH LOG;

you will need sysadmin or alter trace to use with log(https://msdn.microsoft.com/en-us/library/ms178592.aspx)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.