要在邮件发送ERROR_MESSAGE(),您需要将其转换为VARCHAR示例下面应该为你工作更新的电子邮件地址和邮件配置文件使用后:
Declare @IsError int = 0
Declare @ErrorMessage VARCHAR(MAX) = ''
BEGIN TRY
-- Send email
EXEC msdb.dbo.sp_send_dbmail @recipients = '[email protected]'
, @from_Address = 'sqlemail.abc.com'
, @body = 'Email Body'
, @subject = 'Email Subject'
, @body_format = 'HTML'
, @profile_name = 'DBProfileName'
SET @IsError = @@ERROR
END TRY
BEGIN CATCH
-- Store exception information casting error message to varchar
SELECT @IsError = 1, @ErrorMessage = 'Exception message: ' + Cast (Error_Message() as varchar) + ';'
END CATCH
IF @IsError <> 0
BEGIN
DECLARE @Subject VARCHAR(250)
SELECT @Subject = 'Server Name: ' + @@SERVERNAME + '; Database Name: ' + DB_NAME() + ';' + ' - Email failed'
EXEC msdb.dbo.sp_send_dbmail @recipients = '[email protected]'
, @from_Address = 'sqlemail.abc.com'
, @body = @ErrorMessage
, @subject = @Subject
, @profile_name = 'DBProfileName'
Raiserror('Error encountered while sending email', 16, 1);
END
请问您的程序抛出一个异常时,它失败或做它只是返回一个非零返回码? – 3martini
我认为非零错误代码 –