I have successfully configured and able to sent email using 'Database mail' on SQL server 2008 R2 by calling msdb.dbo.sp_send_dbmail stored procedure. And I can catch the failure of a query in a trigger by putting it in try-catch block.
But What I need to do is, Send an email when a trigger fail to execute a query. But I can not execute above stored procedure within the catch block. It gave me following error.
Msg 3930, Level 16, State 1, Procedure sp_send_dbmail, Line 64
The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction.
The statement has been terminated.
Is there any other way to send email on trigger failure? Or how to fix this?
Trigger failure will always cause a transaction rollback and as such it cannot do any transactional operation, like calling
sp_send_dbmail
. One trick is to leverage custom profiler trace events, since they are sent on a standalone transaction: