SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using sp_add_schedule


Using sp_add_schedule

Author
Message
Half Bubble
Half Bubble
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 168
Obviously, I've overlooked something. Any experts see it? I have a stored procedure to schedule a task to run every "RunsEvery" hours. It seems to work, but my "SendBookingReminders" procedure isn't exec'ing every RunsEvery hours.

ALTER PROCEDURE [dbo].[StartBookingReminders]
-- Add the parameters for the stored procedure here
@RunsEvery Int,
@ServerName VARCHAR(30),
@DatabaseName VARCHAR(128)

AS
BEGIN
Exec dbo.StopBookingReminders
exec msdb.dbo.sp_add_job @job_name = 'CS-Booking_Reminders',
@enabled=1
exec msdb.dbo.sp_add_jobstep
@job_name = 'CS-Booking_Reminders',
@step_name = 'Check for reminders to send',
@subsystem = 'TSQL',
@command = 'exec dbo.SendBookingReminders',
@database_name = @databaseName
exec msdb.dbo.sp_add_schedule
@schedule_name = 'CS-Booking_Reminders_Schedule',
@enabled = 1,
@freq_subday_type = 8,
@freq_type = 128,
@freq_subday_interval = @RunsEvery
exec msdb.dbo.sp_attach_schedule
@job_name = 'CS-Booking_Reminders',
@schedule_name = 'CS-Booking_Reminders_Schedule'


END

Halfbubble ------------
You just can't do this stuff unless you're at least half a bubble off center.

Jack Corbett
  Jack Corbett
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19226 Visits: 14900
Is the job running at all?

What is returned if you run a query like this:

SELECT
*
FROM
msdb.dbo.sysjobhistory AS JH JOIN
msdb.dbo.sysjobs AS J
ON JH.job_id = J.job_id
WHERE
J.NAME = 'CS-Booking_Reminders'





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Half Bubble
Half Bubble
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 168
Nope. The job isn't running. I don't find it listed in the sysjobhistory list, though it is in the sysjobs table.

Halfbubble ------------
You just can't do this stuff unless you're at least half a bubble off center.

Half Bubble
Half Bubble
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 168
I tried adding msdb.dbo.sp_add_jobserver to my start procedure


ALTER PROCEDURE [dbo].[StartBookingReminders]
-- Add the parameters for the stored procedure here
@RunsEvery Int,
@ServerName VARCHAR(30),
@DatabaseName VARCHAR(128)

AS
BEGIN
Exec dbo.StopBookingReminders
exec msdb.dbo.sp_add_job @job_name = 'CS-Booking_Reminders',
@enabled=1
exec msdb.dbo.sp_add_jobstep
@job_name = 'CS-Booking_Reminders',
@step_name = 'Check for reminders to send',
@subsystem = 'TSQL',
@command = 'exec dbo.SendBookingReminders',
@database_name = @databaseName
exec msdb.dbo.sp_add_schedule
@schedule_name = 'CS-Booking_Reminders_Schedule',
@enabled = 1,
@freq_subday_type = 8,
@freq_type = 128,
@freq_subday_interval = @RunsEvery
exec msdb.dbo.sp_attach_schedule
@job_name = 'CS-Booking_Reminders',
@schedule_name = 'CS-Booking_Reminders_Schedule'
exec msdb.dbo.sp_add_jobserver
@job_name = 'CS-Booking_Reminders',
@server_name = @ServerName

END



Haven't seen any results in the job history yet


SELECT
*
FROM
msdb.dbo.sysjobhistory AS JH JOIN
msdb.dbo.sysjobs AS J
ON JH.job_id = J.job_id
WHERE
J.NAME = 'CS-Booking_Reminders'



Halfbubble ------------
You just can't do this stuff unless you're at least half a bubble off center.

Half Bubble
Half Bubble
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 168
The End of the Matter:


ALTER PROCEDURE [dbo].[StartBookingReminders]
-- Add the parameters for the stored procedure here
@RunsEvery Int,
@ServerName VARCHAR(30),
@DatabaseName VARCHAR(128)

AS
BEGIN
Exec dbo.StopBookingReminders
exec msdb.dbo.sp_add_job @job_name = 'CS-Booking_Reminders',
@enabled=1
exec msdb.dbo.sp_add_jobstep
@job_name = 'CS-Booking_Reminders',
@step_name = 'Check for reminders to send',
@subsystem = 'TSQL',
@command = 'exec dbo.SendBookingReminders',
@database_name = @databaseName
exec msdb.dbo.sp_add_schedule
@schedule_name = 'CS-Booking_Reminders_Schedule',
@enabled = 1,
@freq_interval = 1,
@freq_type = 4,
@freq_subday_type = 8,
@freq_recurrence_factor = 1,
@freq_subday_interval = @RunsEvery
exec msdb.dbo.sp_attach_schedule
@job_name = 'CS-Booking_Reminders',
@schedule_name = 'CS-Booking_Reminders_Schedule'
exec msdb.dbo.sp_add_jobserver
@job_name = 'CS-Booking_Reminders',
@server_name = @ServerName

END



The trick is in getting the Add Schedule call correct. To get something to happen each hour every day, you need to set:

@freq_interval = 1,
@freq_type = 4,
@freq_subday_type = 8,
@freq_recurrence_factor = 1,
@freq_subday_interval = @RunsEvery

The scheduler looks at the @freq_interval and @freq_type to decide which days to run it on, then, on those days, it looks at @freq_subday_type and @freq_subday_interval to decide when to run it on those days.

If your recurrence_faction is 0, the scheduler will run it the first time on the first day, and not again.

Halfbubble ------------
You just can't do this stuff unless you're at least half a bubble off center.

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search