• 通过存储过程创建SQL作业


    USE dbName
    GO
    /****** Object: StoredProcedure [dbo].[usp_Createjob] Script Date: 03/26/2014 14:36:30 ******/
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_Createjob]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[usp_Createjob]
    GO
    create proc usp_Createjob
    @sql varchar(8000), --要执行的命令
    @freqtype varchar(6)='day', --时间周期,month 月,week 周,day 日
    @fsinterval int=1, --相对于每日的重复次数
    @time int=233000 --开始执行时间,对于重复执行的作业,将从0点到23:59分
    as
    --判断作业是否存在
    if exists(select * from msdb.dbo.sysjobs
    where name='HistoryManagerPlan')
    begin
    --如果作业存在则先删除原来作业
    EXECUTE msdb..sp_delete_job @job_name = 'HistoryManagerPlan'
    end
    --创建作业
    exec msdb..sp_add_job @job_name='HistoryManagerPlan'

    --创建作业步骤
    exec msdb..sp_add_jobstep @job_name='HistoryManagerPlan',
    @step_name = '數據處理',
    @subsystem = 'TSQL',
    @database_name=[BTLife_History],--默认为当前的数据库名
    @command = @sql,
    @retry_attempts = 5, --重试次数
    @retry_interval = 5 --重试间隔

    --创建调度
    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 @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 @job_name='HistoryManagerPlan',
    @name = '時間安排',
    @freq_type=@ftype , --4 每天,8 每周,16 每月
    @freq_interval=1, --重复执行次数
    @freq_subday_type=@fstype, --是否重复执行
    @freq_subday_interval=@fsinterval, --重复周期
    @freq_recurrence_factor=@ffactor,
    @active_start_time=@time --下午23:30:00分执行

    EXEC msdb..sp_add_jobserver @job_name = 'HistoryManagerPlan',
    @server_name = N'(local)'
    go

    ----------------調用執行儲存過程創建作業----------------------------
    exec dbName.dbo.usp_Createjob
    @sql='exec usp_DeleteHistory @Days=''90''',
    @time='155000'

  • 相关阅读:
    JavaScript总结(一)
    序列化函数
    random与os,sys模块
    认识模块
    时间模块
    日志处理
    异常处理
    类的约束
    反射
    区分函数以及方法
  • 原文地址:https://www.cnblogs.com/mingdep/p/5065744.html
Copyright © 2020-2023  润新知