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  
  
  declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for  
  select tsk_id,tsk_command,tsk_type   from mps_task_kernel  with(nolock) where tsk_times<10 order by tsk_id 

  open status1  
  declare @sql nvarchar(4000)   
  declare @taskid bigint   
  declare @type varchar(50) 
  declare @newordid bigint   
  FETCH NEXT FROM status1 INTO @taskid,@sql ,@type  
  WHILE (@@FETCH_STATUS = 0)  
  BEGIN  
    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 
if (@newordid>0) 
begin
delete mps_task_kernel  where tsk_id=@taskid 
end else
begin
update mps_task_kernel with(rowlock) set  tsk_times = tsk_times+1 where tsk_id=@taskid 
end
        end  else 
        begin
exec sp_executesql @sql 
delete mps_task_kernel  where tsk_id=@taskid 
        end 

    COMMIT TRAN -------提交事务
    END TRY-----------结束捕捉异常
    BEGIN CATCH------------有异常被捕获
    
        
        
        IF @@TRANCOUNT > 0---------------判断有没有事务
        BEGIN
            ROLLBACK TRAN----------回滚事务
        END 
        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(),ERROR_PROCEDURE(),
        ERROR_LINE(),ERROR_MESSAGE() 
        update mps_task_kernel with(rowlock) set  tsk_times = tsk_times+1 where tsk_id=@taskid 
    END CATCH--------结束异常处理
    end
    FETCH NEXT FROM status1 INTO @taskid,@sql ,@type  
  END  
  CLOSE status1  
 DEALLOCATE status1   
   
end