BEGIN TRY
BEGIN TRANSACTION
--Lots of T-SQL Code here
COMMIT
END TRY
BEGIN CATCH
ROLLBACK
USE [msdb];
EXEC sp_send_dbmail
@profile_name='Mail Profile',
@recipients='[email protected]',
@subject='Data Error',
@body = SELECT ERROR_MESSAGE();
END CATCH
I am getting the following error at this line
@body = SELECT ERROR_MESSAGE();
Incorrect syntax near the keyword 'SELECT'.
Any one know why?
When called in a CATCH block, ERROR_MESSAGE returns the complete text of the error message that caused the CATCH block to run. The text includes the values supplied for any substitutable parameters - for example, lengths, object names, or times. ERROR_MESSAGE returns NULL when called outside the scope of a CATCH block.
Using @@ERROR to conditionally exit a procedure. The following example uses IF...ELSE statements to test @@ERROR after an DELETE statement in a stored procedure. The value of the @@ERROR variable determines the return code sent to the calling program, indicating success or failure of the procedure.
CATCH construct cannot span multiple blocks of Transact-SQL statements.
ERROR_LINE returns the line number at which the error occurred. This happens regardless of the location of the ERROR_LINE call within the scope of the CATCH block, and regardless of the number of calls to ERROR_LINE .
You can not issue a SELECT statement directly into the parameter of a stored procedure. Do something like this instead:
DECLARE @err_msg AS NVARCHAR(MAX);
SET @err_msg = ERROR_MESSAGE();
EXEC sp_send_dbmail
@profile_name='your Mail Profile here',
@recipients='[email protected]',
@subject='Data Error',
@body=@err_msg
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With