After a job was added, the job is shown in Management Studio (SSMS) in the Object Explorer under SQL Server Agent -> Jobs.
Add one or more job steps
A job by itself doesn't do much. It is a container of job steps. It's these job steps that actually define what will be executed.
declare
@db_name sysname = db_name();
exec msdb..sp_add_jobstep
@job_name = N'tq84_job_example_job',
@step_name = N'tq84_job_example_step',
@step_id = 1, -- Used to order steps.
@subsystem = N'TSQL',
@database_name = @db_name,
@command = N'exec tq84_job_example_proc',
@on_success_action = 1, -- 1:... / 3: Go to next step (3 should not be the value of the last step)
@retry_attempts = 0,
@retry_interval = 0
;
A schedule defines when, how often and which periods etc. a job is executed. Unfortunately, it's quite unintuitive, imho, how such a schedule is defined:
select
his.run_date,
his.run_time,
his.run_duration,
his.instance_id,
his.step_id,
his.step_name,
his.run_status,
his.sql_message_id,
his.sql_severity,
his.message,
his.server
from
msdb.dbo.sysjobs job left join
msdb.dbo.sysjobhistory his on job.job_id = his.job_id
where
job.name = 'tq84_job_example_job'
order by
his.instance_id desc
;
Some information about job steps is stored in msdb.dbo.sysjobsteps:
select
stp.step_uid,
stp.step_id,
stp.step_name,
stp.subsystem,
stp.additional_parameters,
stp.database_name step_database,
stp.retry_attempts,
stp.retry_interval,
stp.last_run_date last_run_date_step,
stp.last_run_time last_run_time_step,
case stp.on_success_action when 3 then 'go on with next step' end on_success_acction
from
msdb..sysjobs job left join -- created jobs
msdb..sysjobsteps stp on job.job_id = stp.job_id
where
job.name = 'tq84_job_example_job'
order by
stp.step_id;