zoukankan      html  css  js  c++  java
  • 通过存储过程创建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'

  • 相关阅读:
    ATM项目分析
    Python常用模块大全
    一文了解@Conditional注解说明和使用
    Spring IOC源码分析之-刷新前的准备工作
    Spring Cloud Zuul API服务网关之请求路由
    ArrayList相关方法介绍及源码分析
    记一次序列化的JSON解析问题
    大型网站架构演化发展历程
    Spring Cloud Ribbon负载均衡
    Spring Cloud Hystrix 服务容错保护
  • 原文地址:https://www.cnblogs.com/mingdep/p/5065744.html
Copyright © 2011-2022 走看看