What is Job in SQL Server?
A job in SQL server is basically a bunch of tasks (queries) performed sequentially by SQL Server Agent.
You also can create a job in sql server by running following query in sql query manager.
EXEC dbo.sp_add_job @job_name = N'Monthly Sales Report'; GO EXEC sp_add_jobstep @job_name = N'Monthly Sales Report', @step_name = N'Set database to read only', @subsystem = N'TSQL', @command = N'ALTER DATABASE SALES SET READ_ONLY', @retry_attempts = 5, @retry_interval = 5; GO
There are some built-in stored procedure to create job, schedule job, attach and add job server.
Normally SQL Jobs are created for automation, means things can be carried out without further human interaction, but there are situation where jobs needs to be executed on demand or in some conditional situation.
Here we create a simple store procedure to execute JOB
CREATE PROC mySpToExecutejob AS EXEC dbo.sp_start_job N'Monthly Newletter'; GO
Now you can call the store procedure from SQL code or execute directly
EXEC mySpToExecutejob
Learn more about SQL Jobs using SQL Server management studio.