|
|
|
Ten 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;
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Yesterday @ 11:14 PM
Points: 21,832,
Visits: 27,862
|
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: 2 days ago @ 10:43 AM
Points: 90,
Visits: 294
|
|
Thanks for the update.. I will wait to hear from you.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, October 24, 2012 9:38 PM
Points: 9,
Visits: 206
|
|
| Hi, i also facing this problem, is there any solution for this ?
|
|
|
|
|
Forum 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,
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Yesterday @ 12:30 PM
Points: 8,
Visits: 132
|
|
| 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.
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: 2 days ago @ 10:43 AM
Points: 90,
Visits: 294
|
|
Lynn, Did you get a chance to find this out? This is still a problem for me..
Thanks.
|
|
|
|