Creation of the database job [timed execution Task] (reprint)

Source: Internet
Author: User

--Monthly job execution
exec p_createjob @jobname = ' mm ', @sql = ' select * from syscolumns ', @freqtype = ' month '
--Jobs performed weekly
exec p_createjob @jobname = ' ww ', @sql = ' select * from syscolumns ', @freqtype = ' Week '
--Daily job execution
exec p_createjob @jobname = ' a ', @sql = ' select * from syscolumns '
--daily jobs that are repeated every 4 hours
exec p_createjob @jobname = ' B ', @sql = ' select * from syscolumns ', @fsinterval =4
--*/
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ P_createjob] ') and OBJECTPROPERTY (ID, N ' isprocedure ') = 1)
drop procedure [dbo]. [P_createjob]
GO
Create proc P_createjob
@jobname varchar,--Job name
@sql varchar (8000),--the command to execute
@dbname sysname= ',--implicitly considers the current database name
@freqtype varchar (6) = ' Day ',--time period, month months, week week
@fsinterval int=1,--Relative to the number of repetitions per day
@time int=170000--start execution time, for jobs that are executed repeatedly, from 0 to 23:59.
As
If IsNull (@dbname, ') = ' Set @dbname =db_name ()
--Create Job
EXEC msdb: Sp_add_job @[email protected]
--Create a job step
EXEC msdb: Sp_add_jobstep @[email protected],
@step_name = ' data processing ',
@subsystem = ' TSQL ',
@[email protected],
@command = @sql,
@retry_attempts = 5,--Retry count
@retry_interval = 5--Retry interval
--Create a schedule
declare @ftype int, @fstype int, @ffactor int
Select @ftype =case @freqtype when "Day" then 4
When the ' Week ' then 8
When the ' month ' then the end
, @fstype =case @fsinterval when 1 then 0 else 8 end
If @fsinterval <>1 set @time =0
Set @ffactor =case @freqtype when "day" then 0 else 1 end
EXEC msdb: Sp_add_jobschedule @[email protected],
@name = ' time schedule ',
@[email protected],--daily, 8 per week, 16 per month
@freq_interval =1,--Repeat execution times
@[email protected],--repeated execution
@[email protected],--repetition cycle
@[email protected],
@[email protected]--17:00:00 minute execution
--Add target server
EXEC Msdb.dbo.sp_add_jobserver
@job_name = @jobname,
@server_name = N ' (local) '
Go

Creation of the database job [timed execution Task] (reprint)

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.