Schedule running stored procedure every 3 hours (T-SQL)

Hi all,

I worked with many databases like Oracle, MySql, Postgres...
And SQL Server as well, long time no see. Also this time I'm under linux and using Azure Data Studio, so no UI to schedule jobs available. So... forgive me for my newbie question below.

How do I prepare T-SQL to schedule running stored procedure every 3 hours (every day, starting from now, no expiration date)?

Tried to follow microsoft doc (can't provide link as I'm fresh here), but it is rather not intuitive and each parameter is not explained in T-SQL.

Please advise.

1 Like

Create a SQL job
Set the time to run every 3 hours
In the step select the db of sproc
Add exec sproc_name

1 Like

See my job script below (I'm about to run it every two hours).

USE [tax]
EXEC msdb.dbo.sp_add_job @job_name=N'backup_tax',
@category_name=N'[Uncategorized (Local)]',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
select @jobId
EXEC msdb.dbo.sp_add_jobserver @job_name=N'backup_tax', @server_name = N'marchelloG' ---CHANGE IT
USE [tax]
EXEC msdb.dbo.sp_add_jobstep @job_name=N'backup_tax', @step_name=N'sp',
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'Exec BackupDatabase @fullPath = ''/mnt/hdd/tmp/backup_tax.bak''',
USE [tax]
EXEC msdb.dbo.sp_update_job @job_name=N'backup_tax',
@category_name=N'[Uncategorized (Local)]',
USE [tax]
DECLARE @schedule_id int
EXEC msdb.dbo.sp_add_jobschedule @job_name=N'backup_tax', @name=N'every 2 hours',
@active_end_time=235959, @schedule_id = @schedule_id OUTPUT
Select @schedule_id

I ran it manually and it works fine

Exec msdb.dbo.sp_start_job 'backup_tax';

But when I check next scheduled run date, it says 2019-04-14 [corrected] (4 days to wait), but why?

, AS scheduleName
, sja.next_scheduled_run_date
FROM msdb.dbo.sysjobs sj
INNER JOIN msdb.dbo.sysjobactivity sja ON sja.job_id = sj.job_id
INNER JOIN msdb.dbo.sysjobschedules sjs ON sjs.job_id = sja.job_id
INNER JOIN msdb.dbo.sysschedules ss ON ss.schedule_id = sjs.schedule_id
WHERE ( LIKE 'backup%')

Please advise.

what is todays date where you are living? are you on another planet ? :wink:

Sorry, my bad. It says 2019-04-14 00:00:00.000 (almost 4 days to wait). What did I do wrong in my job script (see above) ?

you have it set to run weekly on Sundays only. 4 days from today = Sunday
change it to run not Weekly but Daily.

Any idea how do I fix it in my script above? I'm under linux and using Azure Data Studio, no access to jobs UI. Thanks anyway...

change from @freq_type=8 to @freq_type=4,

1 Like

Thanks yosiasz.

So, I deleted job and re-created it with @freq_type=4.
Checked next scheduled run date and it was NULL.
Ran job manually and it worked fine, then checked next scheduled run date and it was 2019-04-11 00:00:00.000
Basically, I can live with that workaround (run job manually after creating).
Still, if there is any idea how to do it properly without need to run manually, please advise.

well it depends where on planet earth you are at this moment and what time it is on your side of the world. what time is your sql server on?

My local time and date:


2019-04-10 22:31:46.3549993

But I do not understand why my next scheduled run date is NULL if I do not run job manually. Is it by design?

because it has not run yet

1 Like

So, it is by design.
Thanks for your help, yosiasz.