SQL 2000 Job Scheduling (scheduled execution of stored procedures)

Source: Internet
Author: User

I read the help documentation of mssql to facilitate later use!
 

If exists (select * from dbo. sysobjects where id = object_id (n' [dbo]. [Pro_AddTask] ') and OBJECTPROPERTY (id, n' IsProcedure') = 1)
Drop procedure [dbo]. [Pro_AddTask]
GO
Create proc Pro_AddTask
@ Taskname varchar (100), -- task name
@ SQL varchar (8000), -- slq command
@ Dbname sysname = '', -- default the current db
@ Freqtype varchar (6) = 'day', -- the frequence of time: month, week, day
@ Freqsubtype varchar (6) = 'no', -- no, hour, minute
@ Fqinterval int = 1, -- the execute times
@ Fqsubinterval int = 1, -- the sub interval
@ Time int = 000000 -- execute on some time format (hhmmss)
As
/* -- Creat task

-- Author: sky 2008.02.27

Discription:
When @ freqtype = 'Week 'when @ fqinterval =
1 = Sunday
2 = Monday
4 = Tuesday
8 = Wednesday
16 = Thursday
32 = Friday
64 = Saturday
*/
/* -- Eg.

-- Jobs executed every month
Exec Pro_AddTask @ taskname = 'mm', @ SQL = 'select * from syscolumns ', @ freqtype = 'month'

-- Weekly jobs
Exec Pro_AddTask @ taskname = 'ww ', @ SQL = 'select * from syscolumns', @ freqtype = 'Week'

-- 00000 of jobs executed per day
Exec Pro_AddTask @ taskname = 'AB', @ SQL = 'select * from syscolumns'

-- Job at 12:00:00 every day
Exec Pro_AddTask @ taskname = 'd12', @ SQL = 'select * from syscolumns ', @ time = 12000

-- Assignments are made at 12:00:00 every Saturday.
Exec Pro_AddTask @ taskname = 'w12', @ SQL = 'select * from syscolumns ', @ freqtype = 'Week', @ fqinterval = 64, @ time = 12000

-- Homework at 12:00:00 on the 20th of every month
Exec Pro_AddTask @ taskname = 'm20', @ SQL = 'select * from syscolumns ', @ freqtype = 'month', @ fqinterval = 20, @ time = 12000

--*/
If isnull (@ dbname, '') ='' set @ dbname = db_name ()
-- Create task
Exec msdb .. sp_add_job @ job_name = @ taskname

-- Create task step
Exec msdb .. sp_add_jobstep @ job_name = @ taskname,
@ Step_name = 'data process ',
@ Subsystem = 'tsql ',
@ Database_name = @ dbname,
@ Command = @ SQL,
@ Retry_attempts = 5,
@ Retry_interval = 5

-- Create schedule
Declare @ ftype int, @ fstype int, @ ffactor int
Select @ ftype = case @ freqtype
When 'day' then 4
When 'Week' then 8
When 'month' then 16
End
, @ Fstype = case @ freqsubtype
When 'no' then 1
When 'hour' then 4
When 'minutue' then 8
Else 0
End
Set @ ffactor = case @ freqtype when 'day' then 0 else 1 end

EXEC msdb .. sp_add_jobschedule @ job_name = @ taskname,
@ Name = 'schedule ',
@ Freq_type = @ ftype, --- 4 daily, 8 weeks, 16 months
@ Freq_interval = @ fqinterval, -- number of days for job execution
@ Freq_subday_type = @ fstype, -- whether to execute repeatedly. 0x1 is at the specified time, 0x4 minutes, 0x8 hours.
@ Freq_subday_interval = @ fqsubinterval, -- recurrence
@ Freq_recurrence_factor = @ ffactor, -- Repeated execution, set to 1; otherwise, set to 0.
@ Active_start_time = @ time -- execute at 00:00:00 pm
-- Add the goal server
EXEC msdb. dbo. sp_add_jobserver
@ Job_name = @ taskname,
@ Server_name = n' (local )'

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.