Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

2 Answers 2

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:

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

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

 
discard

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.