Check the job running time so that the normal operation of the production database is not affected.

Source: Internet
Author: User
You may find yourself in a position where you need to look at what jobs have been running over a specified
Period of time. SQL server has a stored procedure sp HelpJob that will allow you to look at the jobs and
What their status is currently, but maybe you need to do some additional processing like sending a database
Mail email to someone so that they know to look at the job. Perhaps you want to have the system check
Itself before the start of business each day to ensure that no overnight jobs got stuck and are still running
Which cocould cause degraded performance or problems with processing during the day. The following code
Will work on SQL 2000,200 5, and 2008.


Jobid to processid Function

This function is used to convert the sysjobs. jobid field into the processid that will show up in
Sysprocesses. program_name field for an executing job.

 CREATE FUNCTION dbo.udf_SysJobs_GetProcessid(@job_id uniqueidentifier)RETURNS VARCHAR(8)ASBEGINRETURN (substring(left(@job_id,8),7,2) +substring(left(@job_id,8),5,2) +substring(left(@job_id,8),3,2) +substring(left(@job_id,8),1,2))END 

Return to top

Tsql code to find jobs running over X minutes

The following code will return a row for each job that is currently running and has been running
Over the number of minutes set in the @ maxminutes variable. To adjust the time frame it looks,
Just change this variable value.

 DECLARE @MaxMinutes intSET @MaxMinutes = 30 SELECTp.spid, j.name, p.program_name, isnull(DATEDIFF(mi, p.last_batch, getdate()), 0) [MinutesRunning], last_batchFROM master..sysprocesses pJOIN msdb..sysjobs j ON dbo.udf_sysjobs_getprocessid(j.job_id) = substring(p.program_name,32,8)WHERE program_name like 'SQLAgent - TSQL JobStep (Job %'  AND isnull(DATEDIFF(mi, p.last_batch, getdate()), 0) > @MaxMinutes 

You may find yourself in a position where you need to look at what jobs have been running over a specified
Period of time. SQL server has a stored procedure spHelpJob that will allow you to look at the jobs and
What their status is currently, but maybe you need to do some additional processing like sending a database
Mail email to someone so that they know to look at the job. Perhaps you want to have the system check
Itself before the start of business each day to ensure that no overnight jobs got stuck and are still running
Which cocould cause degraded performance or problems with processing during the day. The following code
Will work on SQL 2000,200 5, and 2008.


Jobid to processid Function

This function is used to convert the sysjobs. jobid field into the processid that will show up in
Sysprocesses. program_name field for an executing job.

 CREATE FUNCTION dbo.udf_SysJobs_GetProcessid(@job_id uniqueidentifier)RETURNS VARCHAR(8)ASBEGINRETURN (substring(left(@job_id,8),7,2) +substring(left(@job_id,8),5,2) +substring(left(@job_id,8),3,2) +substring(left(@job_id,8),1,2))END 

Return to top

Tsql code to find jobs running over X minutes

The following code will return a row for each job that is currently running and has been running
Over the number of minutes set in the @ maxminutes variable. To adjust the time frame it looks,
Just change this variable value.

 DECLARE @MaxMinutes intSET @MaxMinutes = 30 SELECTp.spid, j.name, p.program_name, isnull(DATEDIFF(mi, p.last_batch, getdate()), 0) [MinutesRunning], last_batchFROM master..sysprocesses pJOIN msdb..sysjobs j ON dbo.udf_sysjobs_getprocessid(j.job_id) = substring(p.program_name,32,8)WHERE program_name like 'SQLAgent - TSQL JobStep (Job %'  AND isnull(DATEDIFF(mi, p.last_batch, getdate()), 0) > @MaxMinutes 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.