zoukankan      html  css  js  c++  java
  • Sql Server批量停止作业

    CREATE Proc [dbo].[Proc_StopJob]
    as
    begin
    declare @I int
    declare @JobID uniqueidentifier
    -- 1. create temp table to save jobs status
    create table #job_run_status
    (
       job_id                  uniqueidentifier  not null,
       last_run_date           int               not null,
       last_run_time           int               not null,
       next_run_date           int               not null,
       next_run_time           int               not null,
       next_run_schedule_id    int               not null,
       requested_to_run        int               not null, -- bool
       request_source          int               not null,
       request_source_id       sysname           collate database_default null,
       running                 int               not null, -- bool
       current_step            int               not null,
       current_retry_attempt   int               not null,
       job_state               int               not null
    )
    -- 2. get jobs status
    insert into #job_run_status
    execute master.dbo.xp_sqlagent_enum_jobs 1, 'sa'
    -- 3. get running jobs
    select job_name = j.name
          ,s.*,ROW_NUMBER() over (order by j.name) as ID
          into #RunJob
      from #job_run_status s
              inner join msdb.dbo.sysjobs j
        on s.job_id = j.job_id
     where s.running = 1        -- running = 1
    --4.停止运行作业
    select @I=COUNT(*) from #RunJob 
    
    while @I>0
    begin
     select @JobID=job_id from #RunJob where ID=@I 
     exec msdb.dbo.sp_stop_job '',@JobID
     set @I=@I-1
    end
    end
  • 相关阅读:
    Python_day1
    12/04
    Linux基础笔记
    八:动态规划-未名湖边的烦恼
    七:动态规划-数字三角形
    六:大数运算-减法运算
    五:大数运算-加法运算
    四:大数运算-乘法运算
    三:排序-幸运数字
    二:排序-果园
  • 原文地址:https://www.cnblogs.com/cangowu/p/4097150.html
Copyright © 2011-2022 走看看