Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

is it possible to get the Sql Server 2008 to send an email when a query RAISEERROR is thrown .. or at the very least, when a RAISEERROR with a predetermined error code or codes?

share|improve this question
up vote 5 down vote accepted

You associate an SQL Agent "alert" to detect the error, which then sends an email to an "operator" or runs a job.

You use sp_add_alert but the main info is here: Monitoring and Responding to Events

share|improve this answer
cheers :) this is more what i'm after. thanks! – Pure.Krome Aug 14 '09 at 5:05

Yes, you can put a try/catch block in and send an email using database mail. It'd go something like this:

   EXEC sp_send_dbmail @profile_name='MyProfile',
      @subject='Query Error',
      @body='An error occurred during execution of a statement.'

You can use @@Error in the CATCH block to detect the specific error level that was thrown.

Here's more on the TRY/CATCH statement
Here's more on SQL Server 2008 Database Mail

share|improve this answer
Cheers for the answer :) What about an auto way, without having to manually putting that code in each stored proc/query ... like .. some autotrigger event thing? – Pure.Krome Aug 14 '09 at 3:33
Ah, yes. I'm with you now. gbn's got it in his answer. Alerts are the way to go for general purpose things. – squillman Aug 14 '09 at 4:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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