IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_delete_permission]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_delete_permission]
GO
create procedure [dbo].[mps_delete_permission] 
@cmpid int ,
@mpids varchar(1000)
as
begin
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @s1 varchar(1000) 
declare @s2 varchar(100)
declare @i int 
declare @t table(mpid int) 
set @s1 = @mpids 
while (@s1>'') 
begin
set @i = charindex(',',@s1) 
if (@i>0) 
begin
set @s2 = substring(@s1,1,@i-1) 
set @s1 = substring(@s1,@i+1,len(@s1)-@i) 
end else 
begin
set @s2 = @s1 
set @s1='' 
end 
if (@s2>'') 
begin
insert into @t(mpid) values(convert(int,@s2)) 

declare @mptype varchar(255), @mpctgid int, @ppid int
select top 1 @mptype=mp_type,@mpctgid = mp_ctgid,@ppid = mp_ppid from  mps_permission 
where mp_id = (convert(int,@s2))

if (@mptype like 'prd%' and @mpctgid >0)
begin
exec mps_up_prdctg_power @cmpid,@mpctgid,@mptype,@ppid,'delete'
end
end 
end 

update mps_biztype set biz_changed = 0 
from mps_permission , @t 
where mp_type like 'biz%' and mp_ctgid=biz_id and mp_id=mpid 

update mps_flowwork set wrk_changed = 0 
from mps_permission , @t 
where mp_type like 'wrk%' and mp_ctgid=wrk_id  and mp_id=mpid 

update mps_docctg set dc_changed = 0 
from mps_permission , @t 
where mp_type like 'doc%' and mp_ctgid=dc_id  and mp_id=mpid 

update mps_prdctg set pc_changed = 0 
from mps_permission , @t 
where mp_type like 'prd%' and mp_ctgid=pc_id  and mp_id=mpid 

update mps_equctg set ec_changed = 0 
from mps_permission , @t 
where mp_type like 'equ%' and mp_ctgid=ec_id  and mp_id=mpid 

update mps_csttype set ct_changed = 0 
from mps_permission , @t 
where mp_type like 'cst%' and mp_ctgid=ct_id  and mp_id=mpid 

update mps_ext_resource set etc_changed = 0 
from mps_permission , @t 
where mp_type like 'etc%' and mp_ctgid=etc_id  and mp_id=mpid 

update mps_programtype set pt_changed = 0 
from mps_permission , @t 
where mp_type like 'etc%' and mp_ctgid=pt_id  and mp_id=mpid 

delete mps_permission 
from @t 
where mp_id=mpid 

end