IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_execute_kernel_task]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_execute_kernel_task]
GO
 
 
CREATE procedure [dbo].[mps_execute_kernel_task]  
as  
begin  
  
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
  
declare @newordid bigint  
declare @type varchar(40)   
declare @sql nvarchar(4000)   
declare @taskid bigint   
declare @t1 datetime 
declare @t2 datetime 
declare @t0 float 
declare @remove10 int = 0 

-- 写一个标准的任务调度程序 
declare @tsk table(ordid bigint ) 
-- 首先取十条未处理的数据
insert into @tsk(ordid) 
select   top 10 tsk_ordid  from mps_task_kernel with(nolock) 
where tsk_command not like '%mps_sys_calc_%' and tsk_lock=1 and tsk_times<10 
order by tsk_times , tsk_id

-- 将需要数据拷贝出来
declare @t table(tskid bigint, cmd nvarchar(4000),tsktype varchar(20))
insert into @t(tskid,cmd,tsktype) 
select distinct tsk_id ,tsk_command,tsk_type 
from mps_task_kernel  with(nolock) , @tsk
where tsk_times<10 and tsk_command not like '%mps_sys_calc_%' and tsk_ordid=ordid and tsk_lock=1  

if (not exists (select 1 from @t)) 
begin
return 
end   
-- lock me 
    update mps_task_kernel with(rowlock) set tsk_lock=0 from @t where  tsk_id=tskid 
    
    -- 
    declare @done table(tskid bigint , usetime int , ici int) 
    --
    
declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for  
select tskid,cmd,tsktype   from @t order by tskid 

open status1  
FETCH NEXT FROM status1 INTO @taskid,@sql ,@type  
WHILE (@@FETCH_STATUS = 0)  
BEGIN  
set @t1 = GETDATE()  
set @sql = isnull(@sql,'')   
if (len(@sql)>10)   
begin  
BEGIN TRY 
BEGIN TRAN 
if (@type='newbiz') 
begin
set @newordid = 0 
exec sp_executesql @sql ,N'@taskid bigint,@newordid bigint output',@taskid  , @newordid output 
set @t2 = GETDATE()  
set @t0 =  convert(float,@t2) - convert(float,@t1 )
if (@newordid>0) 
begin
insert into @done(tskid,usetime,ici) values(@taskid , convert(int,@t0 * (24 * 3600) * 1000) , 0) 
end else
begin
insert into @done(tskid,usetime,ici) values(@taskid , 0 , 4) 
set @remove10 = @remove10 + 1
end
end  else 
begin
exec sp_executesql @sql 
set @t2 = GETDATE()  
set @t0 =  convert(float,@t2) - convert(float,@t1 )
insert into @done(tskid,usetime,ici) values(@taskid , convert(int,@t0 * (24 * 3600) * 1000) , 0) 
end 

COMMIT TRAN -------提交事务
END TRY-----------结束捕捉异常
BEGIN CATCH------------有异常被捕获
IF @@TRANCOUNT > 0---------------判断有没有事务
BEGIN
ROLLBACK TRAN----------回滚事务
END 
set @t2 = GETDATE()  
set @t0 =  convert(float,@t2) - convert(float,@t1 )
insert into mps_task_error(tske_type,tske_tskid,tske_number,tske_severity,tske_state,tske_procedure,tske_line,tske_message)
SELECT 'kernel',@taskid,ERROR_NUMBER(),ERROR_SEVERITY(),ERROR_STATE(),@sql+char(13)+char(10)+ERROR_PROCEDURE(),
ERROR_LINE(),ERROR_MESSAGE() 
insert into @done(tskid,usetime,ici) values(@taskid , convert(int,@t0 * (24 * 3600) * 1000) , 1) 
set @remove10=@remove10 + 1 
END CATCH--------结束异常处理
end
FETCH NEXT FROM status1 INTO @taskid,@sql ,@type  
END  
CLOSE status1  
DEALLOCATE status1   
-- 清理任务 
update mps_task_kernel with(rowlock)   set tsk_lock=case when ici>0 then 1 else tsk_lock end , tsk_usedtime=tsk_usedtime+usetime,tsk_times=tsk_times+ici from @done where tsk_id=tskid  
delete mps_task_kernel with(rowlock) from @done where tsk_id=tskid and ici=0 

if (@remove10>0)  
begin
delete mps_task_kernel with(rowlock)  where tsk_times>=10 
end 
  
end