IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_flowworkstatus_delete]') AND type = 'TR')
DROP TRIGGER [dbo].[mps_flowworkstatus_delete]
GO

CREATE trigger [dbo].[mps_flowworkstatus_delete] on dbo.mps_flowworkstatus for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return
    
    declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
  select ws_id , ws_swrkid , wrk_bizid,biz_cmpid from deleted , mps_flowwork , mps_biztype 
  where ws_swrkid=wrk_id and wrk_bizid=biz_id 
  open status1
  declare @wsid int 
  declare @wrkid int
  declare @bizid int 
  declare @cmpid int
  declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
declare @sql nvarchar(1000) 
  FETCH NEXT FROM status1 INTO @wsid ,@wrkid,@bizid,@cmpid
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
  exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
   if (dbo.checkprocedure(@tbname+'_'+convert(varchar(10),@wsid)+'_wrklink_after')=0)
   begin
   set @sql='drop procedure '+@tbname+'_'+convert(varchar(10),

@wsid)+'_wrklink_after' 
exec(@sql)
end 
if (dbo.checkprocedure(@tbname+'_'+convert(varchar(10),@wsid)+'_wrklink_before')=0)
   begin
   set @sql='drop procedure '+@tbname+'_'+convert(varchar(10),@wsid)+'_wrklink_before' 
exec(@sql)
end 
   FETCH NEXT FROM status1 INTO @wsid ,@wrkid,@bizid,@cmpid
  END
  CLOSE status1
DEALLOCATE status1 

delete mps_flowworkstatus_history 
from deleted a
where mps_flowworkstatus_history.ws_id=a.ws_id 

insert into mps_flowworkstatus_history(
ws_id,
ws_swrkid,
ws_twrkid,
ws_caption,
ws_condition,
ws_pickvalues,
ws_status,
ws_status_f,
ws_status_ff,
ws_status_r,
ws_key,
ws_key_f,
ws_key_ff,
ws_key_r,
ws_sql,
ws_prevwrkids,
ws_fjbstatus,
ws_bizrelation,
ws_accsource,
ws_acctype,
ws_accdate,
ws_accobjtype,
ws_accobj,
ws_accfid,
ws_accmethod,
ws_accvalue,
ws_accsuspend,
ws_accsql,
ws_tosubflow,
ws_bizid,

ws_title )
select 
ws_id,

ws_swrkid,
ws_twrkid,
ws_caption,
ws_condition,
ws_pickvalues,
ws_status,
ws_status_f,
ws_status_ff,
ws_status_r,
ws_key,
ws_key_f,
ws_key_ff,
ws_key_r,
ws_sql,
ws_prevwrkids,
ws_fjbstatus,
ws_bizrelation,
ws_accsource,
ws_acctype,
ws_accdate,
ws_accobjtype,
ws_accobj,
ws_accfid,
ws_accmethod,
ws_accvalue,
ws_accsuspend,
ws_accsql,
ws_tosubflow ,
wrk_bizid,
flw_title+'/'+wrk_title+'/'+ws_caption 
from deleted , mps_flowwork , mps_bizflow 
where ws_swrkid=wrk_id and wrk_flwid=flw_id 

delete mps_extsqljs_history 
from deleted 
where 
  cd_objid=ws_id and cd_name in ('wrk.afterlinkapply','wrk.beforelinkapply')
  
insert into mps_extsqljs_history(
cd_objid,
cd_objid2,
cd_name,
cd_text)
select 
cd_objid,
cd_objid2,
cd_name,
cd_text
from mps_extsqljs,deleted 
where 
  cd_objid=ws_id and cd_name in ('wrk.afterlinkapply',
'wrk.beforelinkapply')

delete mps_extsqljs
from deleted 
where 
cd_objid=ws_id and cd_name in ('wrk.afterlinkapply',
'wrk.beforelinkapply')


delete mps_flowworkstatus_data_history
from deleted
where wsd_wsid=ws_id 


insert into mps_flowworkstatus_data_history(
wsd_wsid,
wsd_tobjtype,
wsd_tobjid,
wsd_tobjid2,
wsd_sord2tag,
wsd_updatesql,
wsd_sql,
wsd_use ,
wsd_keyfield )
select 
wsd_wsid,
wsd_tobjtype,
wsd_tobjid,
wsd_tobjid2,
wsd_sord2tag,
wsd_updatesql,
wsd_sql,
wsd_use ,
wsd_keyfield 
from mps_flowworkstatus_data ,   deleted
where wsd_wsid=ws_id 

delete mps_flowworkstatus_data
from deleted
where wsd_wsid=ws_id 
  
    return

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