IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_fix_permission_order]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_fix_permission_order]
GO

create procedure [dbo].[mps_fix_permission_order](
@mpids varchar(8000),
@order1 int 
)
as
begin
declare @t table(dpid int, order1 int ) 
declare @i int,@curInd int,@strlen int,@tmps varchar(100) ,@srcs varchar(8000)
set @srcs = @mpids 
declare @ic int 
set @ic = 0
select @curInd=1,@strlen=LEN(@srcs) 
while (@curInd<=@strlen) 
begin 
select @i=charindex(',',@srcs,@curInd) 
if (@i>0) 
begin 
select @tmps=SUBSTRING(@srcs,@curInd,@i-@curInd) 
set @curInd=@i+1 
end else if (@curInd<=@strlen) 
begin 
select @tmps=SUBSTRING(@srcs,@curInd,@strlen-@curInd+1) 
set @curInd=@strlen+1 
end 
if (@tmps>'') 
begin
set @ic = @ic + 1 
insert into @t(dpid,order1) values(convert(int,@tmps),@ic) 
end 
end
if (@order1=0) 
update mps_permission set mp_order = order1 
from @t
where mp_id=dpid 
else 
update mps_permission set mp_order = 0 
from @t
where mp_id=dpid 
end