SQL Server Jobs | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

SQL Server Jobs

Hi,
I am new to SQL server Development/DBA Part. on my work, I am given one task to complete. There are several daily jobs running in our SQL server. There is one Job(Job-A) that I need to look after. as and when that Job-A completes, I need to see if that day is the Last Day of the Month or not? If that is the last day of the month then invoke another Job. Now here is the tricky part. I don’t have to only validate if that day is the Last day of the month. But also I need to make sure that even if that is not the last day of the month, that should be the last day of the Job-A Schedule for the Month. Let me clarify with the example.
Job-A is running for Monday through Thursday in a month.
Now, Lets say, Friday is the Last Working day of this Month. So, if Job-A completes on Thursday for this month, that should be the last day of the month, cause that job is not going to run again in this month. so right away it should invoke Job-B on Thursday. That’s what i want to do. Please Help… Thanks in advance.

Script to check the last day of the month…<br />select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate() ), 0))<br /><br />Here is the script find the next run date…<br /><br /><br /><br />SELECT dbo.sysjobs.Name AS ‘Job Name’, <br />’Job Enabled’ = CASE dbo.sysjobs.Enabled<br />WHEN 1 THEN ‘Yes'<br />WHEN 0 THEN ‘No'<br />END,<br />’Schedule Enabled’ = CASE dbo.sysjobschedules.Enabled<br />WHEN 1 THEN ‘Yes'<br />WHEN 0 THEN ‘No'<br />END,<br />’next_run_date’ = CASE next_run_date<br />WHEN 0 THEN null<br />ELSE<br />substring(convert(varchar(15),next_run_date),1,4) + ‘/’ + <br />substring(convert(varchar(15),next_run_date),5,2) + ‘/’ + <br />substring(convert(varchar(15),next_run_date),7,2)<br />END+’ ‘+ CASE len(next_run_time)<br />WHEN 1 THEN cast(’00:00′<br />–+ Left(right(active_start_time,2),1) <br />+’:0′ + right(active_start_time,1) as char (<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />)<br />WHEN 3 THEN cast(’00:0′ <br />+ Left(right(next_run_time,3),1) <br />+’:’ + right(next_run_time,2) as char (<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />)<br />WHEN 4 THEN cast(’00:’ <br />+ Left(right(next_run_time,4),2) <br />+’:’ + right(next_run_time,2) as char (<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />)<br />WHEN 5 THEN cast(‘0′ + Left(right(next_run_time,5),1) <br />+’:’ + Left(right(next_run_time,4),2) <br />+’:’ + right(next_run_time,2) as char (<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />)<br />WHEN 6 THEN cast(Left(right(next_run_time,6),2) <br />+’:’ + Left(right(next_run_time,4),2) <br />+’:’ + right(next_run_time,2) as char (<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />)<br />END<br />FROM dbo.sysjobs <br />LEFT OUTER JOIN dbo.sysjobschedules ON dbo.sysjobs.job_id = dbo.sysjobschedules.job_id<br />LEFT OUTER JOIN (SELECT job_id, max(run_duration) AS run_duration<br />FROM dbo.sysjobhistory<br />GROUP BY job_id) Q1<br />ON dbo.sysjobs.job_id = Q1.job_id<br />WHERE dbo.sysjobs.Enabled = 1 and dbo.sysjobschedules.Enabled = 1<br />–and dbo.sysjobs.Name = ”<br />ORDER BY [next_run_date] desc,[next_run_time] desc<br /><br /><br />– select * from msdb..sysjobschedules<br /><br /><br />MohammedU.<br />Moderator<br />SQL-Server-Performance.com
Thanks Mohammed.. I hardcoded that anyway.. but it was helpful though…
Once again .. Thanks for your quick response.. Regards, Rupesh

]]>