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

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
insert into mps_task_history(
tsk_cmpid,
tsk_bcid,
tsk_branch,
tsk_dpid,
tsk_depart,
tsk_cuid,
tsk_cuname,
tsk_date,
tsk_objtype,
tsk_type,
tsk_ctgid,
tsk_objid,
tsk_objid2,
tsk_jbid,
tsk_message) 
select 
tsk_cmpid,
tsk_bcid,
tsk_branch,
tsk_dpid,
tsk_depart,
tsk_cuid,
tsk_cuname,
tsk_date,
tsk_objtype,
tsk_type,
tsk_ctgid,
tsk_objid,
tsk_objid2,
tsk_jbid,
tsk_message
from deleted 

    return

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