IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_job_delete]') AND type = 'TR')
DROP TRIGGER [dbo].[mps_job_delete]
GO
   
CREATE trigger  mps_job_delete on mps_job for delete as  
begin      
declare         @numrows  int,         
@errno    int,         
@errmsg   varchar(255)        
select  @numrows = @@rowcount      
if @numrows = 0 return              

insert into mps_job_history(jb_id,jb_cmpid,jb_bcid,jb_dpid,jb_cuid,jb_executor,jb_crmid,jb_wrktype,jb_execnum,   jb_ordtype,jb_ordid,jb_ordno,jb_ordtitle,jb_orddate,jb_ordcreator,jb_wrkid,jb_title,jb_date,   jb_enddate,jb_wsid,jb_wsstatus,jb_isauto,jb_templateid,jb_sign,jb_back,jb_branch,jb_depart,   jb_createcuid,jb_creator,jb_fjbid,jb_text,jb_limitedtime,jb_elapsedtime,jb_backinfo,jb_ordstatus,jb_ordkey)  select jb_id,jb_cmpid,jb_bcid,jb_dpid,jb_cuid,jb_executor,jb_crmid,jb_wrktype,jb_execnum,   jb_ordtype,jb_ordid,jb_ordno,jb_ordtitle,jb_orddate,jb_ordcreator,jb_wrkid,jb_title,jb_date,   jb_enddate,jb_wsid,jb_wsstatus,jb_isauto,jb_templateid,jb_sign,jb_back ,jb_branch,jb_depart,    jb_createcuid,jb_creator,jb_fjbid,jb_text,jb_limitedtime,
jb_elapsedtime,jb_backinfo,jb_ordstatus,jb_ordkey   
from deleted   
where jb_wsid>0        
return    

/*  Errors handling  */  
error:      
---- raiserror @errno @errmsg      
rollback  transaction  
end