SQL Server代理-每个作业执行的唯一ID

时间:2018-07-19 17:55:28

标签: sql sql-server sql-server-agent

我有这个查询,它为我的计划工作提供了很多有用的信息,但是我仍然缺少一件事。我正在尝试为每个作业执行找到唯一的ID,而不是实例或日程表ID。这是否存在,以及如何将其加入到我正在使用的当前表中?感谢您的协助。

谢谢!

编辑:我知道我可以为每天运行一次的作业创建唯一的ID,但是大多数作业每天运行多次。有些每2分钟运行一次。

            SELECT h.instance_id
                , j.job_id
                ,j.name AS JobName
                ,CASE 
                    WHEN h.step_name = '(Job outcome)'
                        THEN 'Job Run Time'
                    ELSE h.step_name
                    END AS StepName
                ,h.step_id
                ,CAST(STR(h.run_date, 8, 0) AS DATETIME) + CAST(STUFF(STUFF(RIGHT('000000' + CAST(h.run_time AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') AS DATETIME) AS StartDatetime
                ,DATEADD(SECOND, ((h.run_duration / 1000000 * 86400 + (h.run_duration - h.run_duration / 1000000 * 1000000) / 10000 * 3600) + (h.run_duration - h.run_duration / 10000 * 10000) / 100 * 60) + (h.run_duration - h.run_duration / 100 * 100), CAST(STR(h.run_date, 8, 0) AS DATETIME) + CAST(STUFF(STUFF(RIGHT('000000' + CAST(h.run_time AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') AS DATETIME)) AS EndDatetime
                ,CASE 
                    WHEN STUFF(STUFF(REPLACE(STR(h.run_duration, 6, 0), ' ', '0'), 3, 0, ':'), 6, 0, ':') > '23:59:00'
                        THEN '23:59:00'
                    ELSE STUFF(STUFF(REPLACE(STR(h.run_duration, 6, 0), ' ', '0'), 3, 0, ':'), 6, 0, ':')
                    END AS run_duration_formatted
                ,((h.run_duration / 1000000 * 86400 + (h.run_duration - h.run_duration / 1000000 * 1000000) / 10000 * 3600) + (h.run_duration - h.run_duration / 10000 * 10000) / 100 * 60) + (h.run_duration - h.run_duration / 100 * 100) AS RunDurationInSeconds
                ,CASE h.run_status
                    WHEN 0
                        THEN 'Failed'
                    WHEN 1
                        THEN 'Succeded'
                    WHEN 2
                        THEN 'Retry'
                    WHEN 3
                        THEN 'Cancelled'
                    WHEN 4
                        THEN 'In Progress'
                    END AS ExecutionStatus
            FROM msdb.dbo.sysjobhistory AS h
            INNER JOIN msdb.dbo.sysjobs AS j ON j.job_id = h.job_id
            LEFT JOIN [msdb].[dbo].[sysjobactivity] A ON A.job_id = h.job_id
            WHERE (j.enabled = 1)
            AND A.session_id = 1053
            AND (CAST(STR(h.run_date, 8, 0) AS DATETIME) + CAST(STUFF(STUFF(RIGHT('000000' + CAST(h.run_time AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') AS DATETIME) >= DATEADD(dd, - 1, CAST(GETDATE() AS DATE)))
                Order by instance_id

2 个答案:

答案 0 :(得分:0)

也许与> Error: package or namespace load failed for ‘rJava’: .onLoad failed > in loadNamespace() for 'rJava', details: call: dyn.load(file, > DLLpath = DLLpath, ...) error: unable to load shared object > '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so': > dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, > 6): Library not loaded: > /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/lib/server/libjvm.dylib > Referenced from: > /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so > Reason: image not found dense_rank()一起使用?

distinct

答案 1 :(得分:0)

编辑:此查询中使用的表每5分钟填充一次我发布的第一个查询中的数据。

好的,我找不到每个计划执行的唯一ID,所以我不得不重新考虑我能做些什么。查询如下。

这解决了我的问题。我在SharePoint中嵌入了一个Google图表,其中列出了作业和运行时间。每个条形图表示作业运行的时间。在此解决方案之前,如果最后一步失败,则图表仅显示失败状态。现在结合了这个功能,我可以看到每个作业执行是否有任何步骤失败。这是太棒了!在下面的图像中,您可以看到红色执行在作业中发生了步骤失败。

        SELECT D.[instance_id]
            ,[JobName]
            ,[StepName]
            ,[step_id]
            ,[Run_Date]
            ,[StartDatetime]
            ,[StartTime]
            ,[EndDatetime]
            ,[End_Time]
            ,[Run_Duration_Formatted]
            ,[RunDurationInSeconds]
            ,MAX(CASE WHEN C.ExecutionStatus IS NULL THEN D.[ExecutionStatus] ELSE C.ExecutionStatus END) AS ExecutionStatus
            FROM [STG_EDW].[dbo].[Job_Runs_FINAL] AS D
            LEFT JOIN 
            (
            SELECT Step0.Instance_ID
            ,'Failed' AS ExecutionStatus
            FROM(
            SELECT [instance_id]
            ,[JobName]
            ,[StartDatetime]
            ,[EndDatetime]
            FROM [My Table]
            WHERE step_id = 0) AS Step0
            INNER JOIN(
            SELECT Instance_ID, JobName, [StartDatetime], [EndDatetime]
            FROM [My Table]
            WHERE ExecutionStatus = 'Failed') AS B ON Step0.JobName = B.JobName AND B.StartDatetime >= Step0.StartDatetime AND B.EndDatetime <= Step0.EndDatetime
            ) AS C ON D.instance_id = C.instance_id
            Group By  D.[instance_id]
            ,[JobName]
            ,[StepName]
            ,[step_id]
            ,[Run_Date]
            ,[StartDatetime]
            ,[StartTime]
            ,[EndDatetime]
            ,[End_Time]
            ,[Run_Duration_Formatted]
            ,[RunDurationInSeconds]

enter image description here