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