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?

link|improve this question

feedback

2 Answers

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

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

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

BEGIN TRY
   [...statement(s)...]
END TRY
BEGIN CATCH
   EXEC sp_send_dbmail @profile_name='MyProfile',
      @recipients='recipient@mydomain.com',
      @subject='Query Error',
      @body='An error occurred during execution of a statement.'
END CATCH

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

link|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
feedback

Your Answer

 
or
required, but never shown

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