IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_pms_any_order]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_pms_any_order]
GO
CREATE procedure [dbo].[mps_save_pms_any_order](
@cmpid int,
@objtype varchar(20),
@ctgid int,
@objid bigint,
@ptype varchar(20),
@values nvarchar(MAX) 
) as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @s nvarchar(MAX) 
set @s = @values 
declare @s1 nvarchar(1000) 
declare @i int 
declare @mptype varchar(20)
declare @mpobj1 varchar(20)
declare @mpobj2 varchar(20)
declare @mp_id int 

set @s1 = @ptype+'>'+CONVERT(varchar(20),@ctgid) 
delete mps_permission where mp_cmpid=@cmpid and mp_type=@s1 and mp_ctgid=@objid 

while (@s>'') 
begin
set @i = CHARINDEX('|',@s) 
if (@i>0) 
begin
set @s1 = SUBSTRING(@s,1,@i-1) 
set @s = SUBSTRING(@s,@i+1,len(@s)-@i) 
end else 
begin
set @s1 = @s 
set @s = '' 
end
set @mptype='' 
set @mpobj1 = '' 
set @mpobj2 = '' 
set @i = CHARINDEX('=',@s1) 
if (@i>0) set @s1 = SUBSTRING(@s1,1,@i-1) 
if (@s1>'') 
begin
set @i = CHARINDEX(',',@s1) 
if (@i>0)
begin
set @mptype = SUBSTRING(@s1,1,@i-1) 
set @s1 = SUBSTRING(@s1,@i+1,1000) 
set @i = CHARINDEX(',',@s1) 
if (@i>0) 
begin
set @mpobj1 = SUBSTRING(@s1,1,@i-1) 
set @mpobj2 = SUBSTRING(@s1,@i+1,100) 
end 
end 
end 
if (@mpobj1>'' and @mpobj2>'' and @mptype>'') 
begin
select top 1 @mp_id=mp_id from mps_permission where mp_cmpid=@cmpid and 
mp_type=@ptype+'>'+CONVERT(varchar(20),@ctgid)  and mp_ctgid=@objid  
and mp_ppid=convert(int,@mptype) and mp_objid1=convert(int,@mpobj1) and mp_objid2=convert(int,@mpobj2)    
set @mp_id=isnull(@mp_id,0) 
if (@mp_id=0)
begin
insert into mps_permission(mp_cmpid,mp_type,mp_ctgid,mp_ppid,mp_objid1,mp_objid2,mp_caption,mp_group,mp_order) 
values(@cmpid,@ptype+'>'+CONVERT(varchar(20),@ctgid) ,@objid , convert(int,@mptype) , 
convert(int,@mpobj1), convert(int,@mpobj2),'',0,0) 
end   
end 
end 
create table #myuser(pptype varchar(30),cuid int, grp int) 
set @s1 = @ptype+'>'+CONVERT(varchar(20),@ctgid) 
exec mps_gen_powerusers @cmpid,0,0,0,0,0,0,0,0,@objtype,@ctgid,@objid,0,@objid,@s1   
select distinct cuid into #t from #myuser
drop table #myuser

--delete mps_permission where mp_cmpid=@cmpid and mp_type=@s1 and mp_ctgid=@objid 

declare @ptype2 varchar(20) 
set @ptype2 = @ptype 
if (charindex('my',@ptype)=1) set @ptype2 = substring(@ptype , 3,30) 

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@ctgid,0,@tbname output,@fkey output ,@dbname output 


declare @sql nvarchar(4000) 
set @sql = N'
delete '+@dbname+@tbname+'_users where ordp_type='''+@ptype2+''' and ordp_ordid='+convert(varchar(10),@objid) +' and ordp_otid=-2  
insert into '+@dbname+@tbname+'_users(ordp_type,ordp_ordid,ordp_cuid,ordp_otid) 
select '''+@ptype2+''' , '+convert(varchar(10),@objid)+',cuid,-2 from #t 
where not exists (select 1 from '+@dbname+@tbname+'_users where 
ordp_type='''+@ptype2+''' and ordp_ordid='+convert(varchar(10),@objid)+' and ordp_cuid=cuid) 
delete '+@dbname+@tbname+'_users where ordp_type='''+@ptype+''' and ordp_ordid='+convert(varchar(10),@objid) +' 
insert into '+@dbname+@tbname+'_users(ordp_type,ordp_ordid,ordp_cuid) 
select '''+@ptype+''' , '+convert(varchar(10),@objid)+',cuid from #t ' 

exec sp_executesql @sql

select COUNT(*) rec_count , 'ok' none from #t 

drop table #t 
set nocount off 
end