SQL SERVER Create Job

Source: Internet
Author: User

--Define Create job
DECLARE @jobid uniqueidentifier
EXEC Msdb.dbo.sp_add_job
@job_name = N ' Job name ',
@job_id = @jobid OUTPUT

--Define job steps
DECLARE @sql nvarchar, @dbname sysname
SELECT @dbname =db_name (),--The job step is executed in the current database
@sql =n '--Job step content '--typically defines a job that is handled with TSQL, which defines the Transact-SQL statement to execute
EXEC Msdb.dbo.sp_add_jobstep
@job_id = @jobid,
@step_name = N ' job step name ',
@subsystem = ' TSQL ',--type of step, typically TSQL
@[email protected],
@command = @sql

--Create a schedule (using several job scheduling templates that are specifically defined later)
EXEC msdb: Sp_add_jobschedule
@job_id = @jobid,
@name = N ' dispatch name ',
@freq_type = 4,--Daily
@freq_interval = 1,--Specify how many days to occur, and this is 1 days.
@freq_subday_type =0x8,--repeat mode, 0x1= at the specified time, 0x4= how many minutes, 0x8= how many hours to execute once
@freq_subday_interval = 1,--repeats the number of cycles, which are performed once per hour
@active_start_date = NULL,--the start date of the job execution, which is null when the current date is formatted as YYYYMMDD
@active_end_date = 99991231,--stop date for job execution, default is 99991231, format YYYYMMDD
@active_start_time = 00000,--The start time of the job execution, in the format HHMMSS
@active_end_time = 235959-The stop time of the job execution, in the format HHMMSS

--Add target server
DECLARE @servername sysname
SET @servername =convert (nvarchar (+), serverproperty (N ' servername '))
EXEC Msdb.dbo.sp_add_jobserver
@job_id = @jobid,
@server_name = @servername--Using the current SQL instance


--Schedule Template definition
--/*--Day Dispatch
EXEC msdb: Sp_add_jobschedule
@job_id = @jobid,
@name = N ' dispatch name ',
@freq_type = 4,--Daily
@freq_interval = 1,--Specify how many days to occur, and this is 1 days.
@freq_subday_type =0x8,--repeat mode, 0x1= at the specified time, 0x4= how many minutes, 0x8= how many hours to execute once
@freq_subday_interval = 1,--repeats the number of cycles, which are performed once per hour
@active_start_date = NULL,--the start date of the job execution, which is null when the current date is formatted as YYYYMMDD
@active_end_date = 99991231,--stop date for job execution, default is 99991231, format YYYYMMDD
@active_start_time = 00000,--The start time of the job execution, in the format HHMMSS
@active_end_time = 235959-The stop time of the job execution, in the format HHMMSS
--*/

--/*--Week Schedule
EXEC Msdb.dbo.sp_add_jobschedule
@job_id = @jobid,
@name = N ' dispatch name ',
@freq_type = 8,--weekly
@freq_recurrence_factor = 1,--every few weeks, this is weekly
@freq_interval = 62,---in the day of the week, represented by POWER (2,n), the value of N is 0~6, which represents Sunday ~ Saturday, if two is specified, the value is added, for example, a value of 65 means execution in Sunday and Sunday (Power (2,0) +power (2 , 6))
@freq_subday_type = 0x8,--repeat mode, 0x1= at the specified time, 0x4= how many minutes, 0x8= how many hours to execute once
@freq_subday_interval = 1,--repeats the number of cycles, which are performed once per hour
@active_start_date = NULL,--the start date of the job execution, which is null when the current date is formatted as YYYYMMDD
@active_end_date = 99991231,--stop date for job execution, default is 99991231, format YYYYMMDD
@active_start_time = 00000,--The start time of the job execution, in the format HHMMSS
@active_end_time = 235959-The stop time of the job execution, in the format HHMMSS
--*/

--/*--Month Schedule (monthly number per x months)
EXEC Msdb.dbo.sp_add_jobschedule
@job_id = @jobid,
@name = N ' dispatch name ',
@freq_type = 16,--monthly
@freq_recurrence_factor = 2,--Every few months, this is every 2 months.
@freq_interval = 2,--executed on the day of the month of execution, this is the 2nd day
@freq_subday_type = 0x8,--repeat mode, 0x1= at the specified time, 0x4= how many minutes, 0x8= how many hours to execute once
@freq_subday_interval = 1,--repeats the number of cycles, which are performed once per hour
@active_start_date = NULL,--the start date of the job execution, which is null when the current date is formatted as YYYYMMDD
@active_end_date = 99991231,--stop date for job execution, default is 99991231, format YYYYMMDD
@active_start_time = 00000,--The start time of the job execution, in the format HHMMSS
@active_end_time = 235959-The stop time of the job execution, in the format HHMMSS
--*/

--/*--Month Schedule (relative time per x months)
EXEC Msdb.dbo.sp_add_jobschedule
@job_id = @jobid,
@name = N ' dispatch name ',
@freq_type = 32,--monthly
@freq_recurrence_factor = 2,--Every few months, this is every 2 months.
@freq_interval = 9,--executed at that time of the month, 1~7= Sunday to Saturday, 8= day, 9 = weekday, 10 = Weekend
@freq_relative_interval = 1,--performed at the first relative time, the allowable value is 1,2,4,8 for the 1th to 4th relative time, and 16 for the last relative time
@freq_subday_type = 0x8,--repeat mode, 0x1= at the specified time, 0x4= how many minutes, 0x8= how many hours to execute once
@freq_subday_interval = 1,--repeats the number of cycles, which are performed once per hour
@active_start_date = NULL,--the start date of the job execution, which is null when the current date is formatted as YYYYMMDD
@active_end_date = 99991231,--stop date for job execution, default is 99991231, format YYYYMMDD
@active_start_time = 00000,--The start time of the job execution, in the format HHMMSS
@active_end_time = 235959-The stop time of the job execution, in the format HHMMSS
--*/

--/*--job scheduling performed at a particular time
EXEC Msdb.dbo.sp_add_jobschedule
@job_id = @jobid,
@name = N ' dispatch name ',
@freq_type = 64--64= runs when the SQLServerAgent service starts, 128= when the computer is idle
--*/

--/*--job scheduling that only executes once
EXEC msdb: Sp_add_jobschedule
@job_id = @jobid,
@name = N ' dispatch name ',
@freq_type = 1,--executes only once
@active_start_date = NULL,--the start date of the job execution, which is null when the current date is formatted as YYYYMMDD
@active_start_time = 00000-The start time of the job execution, in the format HHMMSS
--*/

SQL SERVER Create Job

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.