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

   

CREATE trigger [dbo].[mps_alarm_delete] on [dbo].[mps_alarm] for delete as
begin
   declare @sql nvarchar(1000) 
   declare @alm_objtype varchar(20)
   declare @alm_objid int
   declare @alm_objid2 int
   declare @alm_id int
   declare @cmpid int 
   declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
  select alm_cmpid,alm_objtype,alm_objid,alm_objid2,alm_id  from deleted  
  open status1
  FETCH NEXT FROM status1 INTO @cmpid,@alm_objtype,@alm_objid,@alm_objid2 ,@alm_id
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
  if (@alm_objtype='biz')
  begin
   set @sql = 'order_'+convert(varchar(10),@alm_objid)+'_alarm_'+ convert(varchar(10),@alm_id) 
   delete mps_job_task where wt_cmpid=@cmpid and wt_bizid=@alm_objid and wt_mbid=@alm_id and wt_type='bizalarm'
end else 
if (@alm_objtype='wrk')
  begin
   set @sql = 'order_'+convert(varchar(10),@alm_objid)+'_alarm_'+convert(varchar(10),@alm_objid2)+'_'+convert(varchar(10),@alm_id) 
   delete mps_job_task where wt_cmpid=@cmpid and wt_bizid=@alm_objid and wt_wrkid=@alm_objid2 and wt_mbid=@alm_id and wt_type='wrkalarm'
end else 
if (@alm_objtype='ebs')
  begin
   set @sql = 'ebs_'+convert(varchar(10),@cmpid)+'_alarm_'+convert(varchar(10),@alm_id) 
   delete mps_job_task where wt_cmpid=@cmpid and  wt_mbid=@alm_id and wt_type='ebsalarm'
end
if (dbo.checkprocedure(@sql)=0) 
begin
set @sql = N'drop procedure '+@sql 
execute(@sql)  
end 
   FETCH NEXT FROM status1 INTO @cmpid,@alm_objtype,@alm_objid,@alm_objid2 ,@alm_id
  END
  CLOSE status1
DEALLOCATE status1  
end