catch块用于插入触发器不触发的错误启动SSA工作

问题描述 投票:0回答:1

我设置了一个AFTER INSERT触发器来启动,其执行SSIS包的过程完成后的ETL日志文件报告的SSA工作。对于try ... catch块的语法似乎正确的,但,当它检测到SSA工作它告诉启动已经在运行的错误处理不为错误代码工作。

扳机是一个SQL Server 2012 SP4实例(与SQL 2012(110)的相容性)的用户表。我想处理一个错误(#22022)的SQL作业已经运行,但似乎你不能错误陷阱系统存储过程的执行。

CREATE TRIGGER InterfaceSupport.trg_XMLTrigger_Insert 
  ON  [InterfaceSupport].[XMLLogReaderTriggers]
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;
    BEGIN TRY
        EXEC msdb.dbo.sp_start_job 'Launch job'
    END TRY
    BEGIN CATCH
        IF ERROR_NUMBER()=22022 --Job already running
            EXEC msdb.dbo.sp_send_dbmail     NULL,'[email protected]',NULL,NULL,'Interface XML Log Reader Already     Running','The SSA Job reading Interface logs is already running. The job will     attempt to catch the new request at the end of the current cycle.'
        ELSE
            BEGIN
                DECLARE @Errormsg nvarchar(max)
                SELECT @Errormsg=ERROR_MESSAGE()
                EXEC msdb.dbo.sp_send_dbmail     NULL,'[email protected]',NULL,NULL,'Interface XML Log Reader Spawn     Error',@Errormsg
            END
    END CATCH
END
GO

尽管错误处理块,我发现了以下错误。这是因为如果CATCH块是不存在的。

Msg 22022, LeveSQLServerAgent Error: Request to run job Launch Job (from User xxx) refused because the job is already running from a request by User xxx. l 16, State 1, Line 17
tsql jobs
1个回答
0
投票

这个错误不能被TRY/CATCH不幸中招。

您应该检查作业是否运行,只有当它没有启动,虽然工作可能检查和EXEC声明之间开始英寸

DECLARE @JobID UNIQUEIDENTIFIER = (SELECT job_id FROM msdb.dbo.sysjobs AS J WHERE J.name = 'Launch job')

IF NOT EXISTS (
    SELECT
        'job is running'
    FROM 
        msdb.dbo.sysjobactivity ja 
        INNER JOIN msdb.dbo.sysjobs j ON ja.job_id = j.job_id
    WHERE 
        ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC) AND 
        ja.start_execution_date is not null AND 
        ja.stop_execution_date is null AND
        ja.job_id = @JobID
    )
BEGIN
     EXEC msdb.dbo.sp_start_job @job_name = 'Launch job'
END
ELSE
BEGIN
    EXEC msdb.dbo.sp_send_dbmail     NULL,'[email protected]',NULL,NULL,'Interface XML Log Reader Already     Running','The SSA Job reading Interface logs is already running. The job will     attempt to catch the new request at the end of the current cycle.'
END
© www.soinside.com 2019 - 2024. All rights reserved.