USE [HCITools] GO IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Get_Job_Error_Info]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[Get_Job_Error_Info] GO USE [HCITools] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[Get_Job_Error_Info] @in_debug tinyint = 0, @in_JobName nvarchar(1000), @in_Recipients varchar(250) AS /*============================================================================= Explication du traitement realise par la SP ------------------------------------------- Cette SP sert a récupérer le message d'erreur d'un step de job et de l'envoyer par mail. Contexte d'utilisation ---------------------- Appelée depuis le step Empty Step et Send mail KO des jobs Parametres ---------- @in_debug : non utilisé @in_JobName : nom du job @in_Recipients : mail/profil du/des destinataire(s) Creation : 1.4.16 / RTC Modifications : 04.04.16 / FLA : normalisation de la SP, modification du paramètre d'entrée, modification de l'appel de l'envoi de mail 06.10.16 / FLA : ajout de la gestion des erreurs de query timeout 19.02.21 / SPE : Modification null message (Bug) =============================================================================*/ SET nocount ON; /*------------------- Declaration des variables --------------------*/ DECLARE @message nvarchar(max) /*------------ Affectation des parametres aux variables ------------*/ SET @message = ''; /*-------------------------- Traitement ---------------------------*/ BEGIN TRY SELECT @message = @message + 'STEP NAME: '+ jh.step_name + CHAR(13) + 'ERROR MESSAGE: ' + CHAR(13) + jh.message + CHAR(13) + CHAR(13) FROM msdb..sysjobhistory jh INNER JOIN msdb..sysjobactivity ja on ja.job_id = jh.job_id INNER JOIN msdb..sysjobs j on j.job_id = ja.job_id WHERE j.name = @in_JobName AND run_status = 0 --and ja.stop_execution_date is null-- AND ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC) AND start_execution_date is not null --AND stop_execution_date is null AND msdb.dbo.agent_datetime(run_date, run_time) >= start_execution_date /* Gestion des Query Timeout Error */ if EXISTS (SELECT 1 FROM msdb..sysjobhistory jh INNER JOIN msdb..sysjobactivity ja on ja.job_id = jh.job_id INNER JOIN msdb..sysjobs j on j.job_id = ja.job_id WHERE j.name = @in_JobName AND run_status = 1 --and ja.stop_execution_date is null-- AND ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC) AND start_execution_date is not null AND jh.sql_message_id = 7412 AND msdb.dbo.agent_datetime(run_date, run_time) >= start_execution_date) BEGIN SELECT @message = @message + 'STEP NAME: '+ jh.step_name + CHAR(13) + 'ERROR MESSAGE: ' + CHAR(13) + jh.message + CHAR(13) + CHAR(13) FROM msdb..sysjobhistory jh INNER JOIN msdb..sysjobactivity ja on ja.job_id = jh.job_id INNER JOIN msdb..sysjobs j on j.job_id = ja.job_id WHERE j.name = @in_JobName AND run_status = 1 --and ja.stop_execution_date is null-- AND ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC) AND start_execution_date is not null AND jh.sql_message_id = 7412 AND msdb.dbo.agent_datetime(run_date, run_time) >= start_execution_date END /*---------------------- Evoie du mail ------------*/ IF (@message <> '') EXEC aps_Send_Mail_with_template @in_param_varchar_2 = @in_Recipients, @in_param_message = @message, @in_job_type = 1 /* 0:sans template; 1/NULL:echec; 2:succes; 3:warning; 4:message*/ END TRY BEGIN CATCH /* Traitement des erreurs (sans RaiseError) */ EXEC dbo.get_Error_Info @in_RaiseError = 0 END CATCH ; /*------------------ Retour au programme appelant -----------------*/ RETURN(@@error); GO