IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_permission_delete]') AND type = 'TR')
DROP TRIGGER [dbo].[mps_permission_delete]
GO
create  trigger [dbo].[mps_permission_delete] on [dbo].[mps_permission] for delete as
begin
    
declare @cmpid int 
select top 1 @cmpid = mp_cmpid from  deleted 
set @cmpid = isnull(@cmpid,0)

if (not exists (select 1 from mps_command where cmd_cmpid=@cmpid and cmd_code='rightPart')) 
begin
insert into mps_command(cmd_code,cmd_cmpid) values('rightPart',@cmpid) 
end 
    
end