SQL: Sending email to an operator rather than a recipient in SQL Server

I was answering a question on a forum the other day, and the person asking the question was puzzled about how to send an email to an operator (i.e. msdb operator) rather than to just any other email address.

The reason he wanted to do that was that he wanted to use the Agent operator system to define a name. He didn't want to hard-code a recipient name in his code. By having it as an operator, it could be changed later, independent of the code.

Doing that isn't so hard. You just need to retrieve the operator's email address from msdb.dbo.sysoperators before sending the mail. Here's an example:

And then send the email like this:

And as BWoodburn noted in the comments, there is also a built-in function that you might be able to call:

sp_notify_operator

It offers slightly different options.

I hope that one of these helps someone.

4 thoughts on “SQL: Sending email to an operator rather than a recipient in SQL Server”

Leave a Reply

Your email address will not be published.