Archive

Archive for August, 2013

BI SQL # 161 : SQL Server DBA Scripts : How to find the SQL Server Running Jobs Time Elapsed status

August 30, 2013 Leave a comment

Hi Folks,

In this article we are going to cover How to find the SQL Server Running Jobs Time Elapsed status.

In this post we are going to discuss following points:

  • Problem Statement of SQL Script:
  • Description of SQL Script:
  • SQL Script Output Column
  • SQL Script Code
  • SQL Script Output Screenshot
  • User Level to execute

Problem Statement of SQL Script:

How to find the SQL Server Running Jobs Time Elapsed status?

Description of SQL Script:

The Query fetches the Running jobs and calculates the time for which they are executing.

SQL Script Output Column

image

SQL Script Code

CREATE TABLE #enum_job (
    Job_ID UNIQUEIDENTIFIER
    ,Last_Run_Date INT
    ,Last_Run_Time INT
    ,Next_Run_Date INT
    ,Next_Run_Time INT
    ,Next_Run_Schedule_ID INT
    ,Requested_To_Run INT
    ,Request_Source INT
    ,Request_Source_ID VARCHAR(100)
    ,Running INT
    ,Current_Step INT
    ,Current_Retry_Attempt INT
    ,STATE INT
    )

INSERT INTO #enum_job
EXEC master.dbo.xp_sqlagent_enum_jobs 1
    ,garbage

SELECT R.NAME
    ,R.last_run_date
    ,R.RunningForTime
    ,GETDATE() AS now
FROM #enum_job a
INNER JOIN (
    SELECT j.NAME
        ,J.JOB_ID
        ,ja.run_requested_date AS last_run_date
        ,(DATEDIFF(mi, ja.run_requested_date, GETDATE())
            ) AS RunningFor
        ,CASE LEN(CONVERT(VARCHAR(5), DATEDIFF(MI, JA.
                        RUN_REQUESTED_DATE, GETDATE()) / 60))
            WHEN 1
                THEN '0' + CONVERT(VARCHAR(5), DATEDIFF(mi, ja.
                            run_requested_date, GETDATE()) / 60
                    )
            ELSE CONVERT(VARCHAR(5), DATEDIFF(mi, ja.
                        run_requested_date, GETDATE()) / 60)
            END + ':' + CASE LEN(CONVERT(VARCHAR(5), (
                        DATEDIFF(MI, JA.RUN_REQUESTED_DATE, 
                            GETDATE()) % 60
                        )))
            WHEN 1
                THEN '0' + CONVERT(VARCHAR(5), (
                            DATEDIFF(mi, ja.
                                run_requested_date, GETDATE()
                            ) % 60
                            ))
            ELSE CONVERT(VARCHAR(5), (
                        DATEDIFF(mi, ja.run_requested_date, 
                            GETDATE()) % 60
                        ))
            END + ':' + CASE LEN(CONVERT(VARCHAR(5), (
                        DATEDIFF(SS, JA.RUN_REQUESTED_DATE, 
                            GETDATE()) % 60
                        )))
            WHEN 1
                THEN '0' + CONVERT(VARCHAR(5), (
                            DATEDIFF(ss, ja.
                                run_requested_date, GETDATE()
                            ) % 60
                            ))
            ELSE CONVERT(VARCHAR(5), (
                        DATEDIFF(ss, ja.run_requested_date, 
                            GETDATE()) % 60
                        ))
            END AS RunningForTime
    FROM msdb.dbo.sysjobactivity AS ja
    LEFT OUTER JOIN msdb.dbo.sysjobhistory AS jh ON ja.
        job_history_id = jh.instance_id
    INNER JOIN msdb.dbo.sysjobs_view AS j ON ja.job_id = j.job_id
    WHERE (
            ja.session_id = (
                SELECT MAX(session_id) AS EXPR1
                FROM msdb.dbo.sysjobactivity
                )
            )
    ) R ON R.job_id = a.Job_Id
    AND a.Running = 1

DROP TABLE #enum_job

SQL Script Output Screenshot

image

User Level to execute

300

    Hope you will like How to find the SQL Server Running Jobs Time Elapsed status.

    If you really like reading my blog and understood at least few thing then please don’t forget to subscribe my blog.

If you want daily link and analysis or interesting link go to following website which will give @ your inbox please subscribe our following link resource blog :

Link Resource Website

For More information related to BI World visit my Mentalist Blog

SQL Server Mentalist >> SQL Learning Blog

Business Intelligence Mentalist >> BI World

Infographic Mentalist >> Image worth explaining thousand Words

Microsoft Mentalist >> MVC,ASP.NET, WCF & LinQ

DBA Mentalist >>Advance SQL Server Blog

Microsoft BI Mentalist >> MS BI Development Update

Connect With me on

| FaceBook |Twitter | linkedIn| Google+ | WordPress | RSS |

Advertisements
%d bloggers like this: