Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Invoked Schedule Id of the Job : Urgent! Expand / Collapse
Author
Message
Posted Tuesday, August 11, 2009 2:34 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, February 18, 2013 3:46 AM
Points: 1,422, Visits: 1,883
I seem to understand what you are looking for...however I can't find any tables that store the schedule id from which a job is invoked...

There is a table - msdb.dbo.sysjobactivity - that stores a record for each job during the job execution (as long as the stop_execution_date is not null for a job in this table the job is currently executing).

I tried to see if there is any way to get the schedule that invoked the job when it is being executed but couldn't find anything - except for the horrible hacked code below which I am only attaching as I worked on it for the past hour

The result of this code (best viewed by setting results to text) will display the name of the schedule and schedule id the job is invoked from (while the job is running)

And finally - I have no clue if this is going to help you in any way...


/*

IMPORTANT: Creates a job called TestJob1 (and deletes any existing job with that name first)
This job will keep running at intervals of 10 and 23 seconds
This code will keep running
Stopping the code will not delete the job - it will still be running - delete the job after stopping the execution

*/
-- make the job wait for the specified delay and then do some very
-- meaningful thing like selecting the current date
DECLARE @JobSql nvarchar(max),@Delay varchar(12);
SET @Delay = '00:00:05';
SET @JobSql = 'WAITFOR DELAY '+QUOTENAME(@Delay,'''');
SET @JobSql = @JobSql + CHAR(13)+CHAR(10)+'SELECT getdate()';

DECLARE @JobCmd varchar(200) = @JobSql;
-- SET YOUR SERVER NAME HERE!!!
DECLARE @srvname sysname = '<<PUTSERVERNAMEHERE>>';

IF EXISTS(SELECT 1 FROM msdb.dbo.sysjobs WHERE name = 'TestJob1')
BEGIN
PRINT 'Deleting job';
EXEC msdb.dbo.sp_delete_job @job_name = 'TestJob1';
END
PRINT 'Creating job';
EXEC msdb.dbo.sp_add_job @job_name = 'TestJob1',@owner_login_name='sa';
EXEC msdb.dbo.sp_add_jobstep @job_name='TestJob1',@step_name = 'Step1',@command=@JobCmd;
EXEC msdb.dbo.sp_add_jobserver @job_name='TestJob1',@server_name=@srvname;
-- 10 second interval schedule
EXEC msdb.dbo.sp_add_jobschedule @job_name='TestJob1', @name='TestJob1_Sched10Second',
@enabled=1, @freq_type=4, @freq_interval=1, @freq_subday_type=2, @freq_subday_interval=10;
-- 23 second interval schedule
EXEC msdb.dbo.sp_add_jobschedule @job_name='TestJob1', @name='TestJob1_Sched23Second',
@enabled=1, @freq_type=4, @freq_interval=1, @freq_subday_type=2, @freq_subday_interval=23;


DECLARE @jobname sysname,@jobid uniqueidentifier,@joboutcomemsg nvarchar(1024);
SET @jobname = 'TestJob1';
SELECT @jobid = job_id FROM msdb.dbo.sysjobs WITH (NOLOCK) WHERE name = @jobname;
WHILE(1=1)
BEGIN
WHILE EXISTS(SELECT 1 FROM msdb.dbo.sysjobactivity WHERE job_id = @jobid)
BEGIN
IF ((SELECT stop_execution_date FROM msdb.dbo.sysjobactivity WHERE job_id = @jobid )IS NOT NULL)
BEGIN
IF (@joboutcomemsg IS NOT NULL)
SELECT 'Before Break',@joboutcomemsg JobOutcomeMessage;

WAITFOR DELAY '00:00:01'
BREAK -- job has completed - break out
END
ELSE
BEGIN
SELECT @joboutcomemsg = last_outcome_message FROM msdb.dbo.sysjobactivity T1 INNER JOIN msdb.dbo.sysjobservers T2 ON
T1.job_id = T2.job_id
WHERE T1.job_id = @jobid
IF (@joboutcomemsg IS NOT NULL)
SELECT 'During continue',@joboutcomemsg JobOutcomeMessage;

WAITFOR DELAY '00:00:01'
CONTINUE -- continue waiting
END
END
END;





Post #768966
Posted Wednesday, August 12, 2009 10:01 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:56 AM
Points: 20,815, Visits: 32,748
I didn't forget, I just spent most of last night figuring out a problem I was having here at work. Hopefully tonight I may get some time to look into this.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #769471
Posted Wednesday, August 12, 2009 10:15 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, May 27, 2014 8:48 AM
Points: 90, Visits: 305
Thanks for the update.. I will wait to hear from you.


Post #769488
Posted Sunday, October 7, 2012 7:42 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, June 24, 2013 3:06 AM
Points: 10, Visits: 209
Hi, i also facing this problem, is there any solution for this ?
Post #1369527
Posted Tuesday, October 30, 2012 2:58 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, October 30, 2012 4:58 AM
Points: 4, Visits: 17
Hi, Have you find solution to get schedule ID when its running as I have facing exact same problem,you were facing.
can you please share your solution?

Thanks in advance,
Post #1378619
Posted Tuesday, November 6, 2012 12:44 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, November 22, 2014 4:18 PM
Points: 8, Visits: 181
I'm also having this problem on SQL 2008 R2. You could write an ugly parsing procedure to extract the schedule_id from the message field in the stepid=0 record of sysjobhistory after the job has ran. That's the closest I've been able to come up with.
Post #1381692
Posted Thursday, December 13, 2012 1:19 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, May 27, 2014 8:48 AM
Points: 90, Visits: 305
Lynn,
Did you get a chance to find this out? This is still a problem for me..

Thanks.



Post #1396368
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse