Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Retreive Job Execution Status From Witin a Stored Procedure

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-08-05 : 08:24:48
Catherine writes "I am trying to determing whether or not a certain job is currently running on the server (using the JobName). Depending on the status I find out, I need to continue processing my stored procedure all within transact sql.

How can I retrieve the current execution status of a job and continue processing in transact sql?

Thanks."

jasper_smith
SQL Server MVP & SQLTeam MVY

846 Posts

Posted - 2002-08-05 : 09:01:59
I wrote a proc to check if a job was idle - you can modify it to check other status if you require it. (you can use sp_help_job but its a bitter over the top for just job status)
 

use master
go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_IsJobIdle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_IsJobIdle]
GO


CREATE PROC sp_IsJobIdle @job_name SYSNAME,@idle char(1) OUTPUT
AS
/*
Check if a certain job is idle

Returns @idle char(1)

Returns 'Y' if job is Idle
Returns 'N' if job is not Idle

Author Date
J Smith 08/05/2002

*/
BEGIN
SET NOCOUNT ON

DECLARE @job_id UNIQUEIDENTIFIER
DECLARE @is_idle INT

--Check name exists

IF NOT EXISTS(SELECT 1 FROM msdb.dbo.sysjobs (nolock) where name=@job_name)
BEGIN
RAISERROR('The job name specified does not exist',16,1)
RETURN 1
END

--Temp table to store results of xp_sqlagent_enum_jobs
CREATE TABLE #xp_results (job_id UNIQUEIDENTIFIER NOT NULL,
last_run_date INT NOT NULL,
last_run_time INT NOT NULL,
next_run_date INT NOT NULL,
next_run_time INT NOT NULL,
next_run_schedule_id INT NOT NULL,
requested_to_run INT NOT NULL,
request_source INT NOT NULL,
request_source_id sysname COLLATE database_default NULL,
running INT NOT NULL,
current_step INT NOT NULL,
current_retry_attempt INT NOT NULL,
job_state INT NOT NULL)

--Get the Job ID
EXECUTE msdb.dbo.sp_verify_job_identifiers '@job_name',
'@job_id',
@job_name OUTPUT,
@job_id OUTPUT
--Get Job Details
INSERT INTO #xp_results
EXECUTE master.dbo.xp_sqlagent_enum_jobs 1,'sa', @job_id

--Get Job Status
/*
0 Returns only those jobs that are not idle or suspended.
1 Executing.
2 Waiting for thread.
3 Between retries.
4 Idle.
5 Suspended.
7 Performing completion actions.
*/
SELECT @is_idle=job_state FROM #xp_results
DROP TABLE #xp_results

--If Not Idle return 'N'
IF @is_idle<>4
BEGIN
SELECT @idle='N'
RETURN
END

--If Idle Return 'Y'
SELECT @idle='Y'
RETURN
END
GO



Here's an example of its use




declare @idle char(1)
exec sp_IsJobIdle 'SAP_dataload',@idle OUTPUT

If @idle='N' raiserror('Job running',16,1)
else
begin
-- DO STUFF
end



HTH
Jasper Smith

Edited by - jasper_smith on 08/05/2002 11:32:40
Go to Top of Page

cdsqlgds
Starting Member

1 Post

Posted - 2002-08-05 : 09:15:12
Thank you so much. This helps a lot.
Go to Top of Page
   

- Advertisement -