IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_up_subtable_info]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_up_subtable_info]
GO
CREATE procedure [dbo].[mps_up_subtable_info](
@objtype varchar(10),
@objid int,
@values nvarchar(4000) ,
@ffrsource nvarchar(500) 
)
as
begin
declare @s nvarchar(4000) 
declare @s1 varchar(500) 
declare @i int 
declare @tag2 nvarchar(10) 
declare @cap nvarchar(100) 
declare @grp nvarchar(100) 
declare @pgsize nvarchar(10) 

set @s=@ffrsource 
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
if (@s1>'') 
begin
if (not exists (select * from mps_biztype_source where bizs_type=@objtype and bizs_bizid=@objid and bizs_ord2tag=0 and bizs_source=@s1)) 
begin
insert into mps_biztype_source(bizs_type,bizs_bizid,bizs_ord2tag,bizs_source,bizs_sourcecaption,bizs_sourcefield)
values(@objtype,@objid,0,@s1,@s1,@s1) 
end 
end
end

set @s = @values 
while (@s>'') 
begin
-- tag2|grp|cap|pgsize|.....
set @i = charindex('|',@s) 
set @tag2 = substring(@s,1,@i-1) 
set @s = substring(@s,@i+1,len(@s)-@i) 
set @i = charindex('|',@s) 
set @grp = substring(@s,1,@i-1) 
set @s = substring(@s,@i+1,len(@s)-@i) 
set @i = charindex('|',@s) 
set @cap = substring(@s,1,@i-1) 
set @s = substring(@s,@i+1,len(@s)-@i) 
set @i = charindex('|',@s) 
if (@i>0)
begin
set @pgsize = substring(@s,1,@i-1) 
set @s = substring(@s,@i+1,len(@s)-@i) 
end else 
begin
set @pgsize = @s 
set @s = '' 
end
if (@pgsize='') set @pgsize='0' 
if (@tag2='') set @tag2='0' 
if (@objtype='biz') 
begin
if (exists (select * from mps_biztype2 where biz2_bizid=@objid and biz2_tag=convert(int,@tag2) and biz2_group=@grp)) 
begin
update mps_biztype2 set biz2_caption=@cap , biz2_page=convert(int,@pgsize) where biz2_bizid=@objid and biz2_tag=convert(int,@tag2) and biz2_group=@grp 
end else 
begin
if (exists (select * from mps_biztype2 where biz2_bizid=@objid and biz2_tag=convert(int,@tag2) and biz2_group='')) 
begin
update mps_biztype2 set biz2_caption=@cap , biz2_group=@grp , biz2_page=convert(int,@pgsize) where biz2_bizid=@objid and biz2_tag=convert(int,@tag2) and biz2_group='' 
end else 
begin
insert into mps_biztype2(biz2_bizid,biz2_tag,biz2_group,biz2_caption,biz2_page) 
values(@objid,convert(int,@tag2),@grp,@cap,convert(int,@pgsize)) 
end
end 
if (@grp>'') delete mps_biztype2 where biz2_bizid=@objid and biz2_tag=convert(int,@tag2) and biz2_group='' 
end
if (@objtype='cst') 
begin
if (exists (select * from mps_csttype2 where ct2_ctid=@objid and ct2_tag=convert(int,@tag2) and ct2_group=@grp)) 
begin
update mps_csttype2 set ct2_caption=@cap , ct2_page=convert(int,@pgsize) where ct2_ctid=@objid and ct2_tag=convert(int,@tag2) and ct2_group=@grp 
end else 
begin
if (exists (select * from mps_csttype2 where ct2_ctid=@objid and ct2_tag=convert(int,@tag2) and ct2_group='')) 
begin
update mps_csttype2 set ct2_caption=@cap , ct2_group=@grp , ct2_page=convert(int,@pgsize) where ct2_ctid=@objid and ct2_tag=convert(int,@tag2) and ct2_group=''  
end else 
begin
insert into mps_csttype2(ct2_ctid,ct2_tag,ct2_group,ct2_caption,ct2_page) 
values(@objid,convert(int,@tag2),@grp,@cap,convert(int,@pgsize)) 
end 
end 
if (@grp>'') delete mps_csttype2 where ct2_ctid=@objid and ct2_tag=convert(int,@tag2) and ct2_group='' 
end
if (@objtype='etc' or @objtype='ext') 
begin
if (exists (select * from mps_ext_resource2 where etc2_etcid=@objid and etc2_tag=convert(int,@tag2) and etc2_group=@grp)) 
begin
update mps_ext_resource2 set etc2_caption=@cap , etc2_page=convert(int,@pgsize) where etc2_etcid=@objid and etc2_tag=convert(int,@tag2) and etc2_group=@grp 
end else 
begin
if (exists (select * from mps_ext_resource2 where etc2_etcid=@objid and etc2_tag=convert(int,@tag2) and etc2_group='')) 
begin
update mps_ext_resource2 set etc2_caption=@cap , etc2_group=@grp , etc2_page=convert(int,@pgsize) where etc2_etcid=@objid and etc2_tag=convert(int,@tag2) and etc2_group=''  
end else 
begin
insert into mps_ext_resource2(etc2_etcid,etc2_tag,etc2_group,etc2_caption,etc2_page) 
values(@objid,convert(int,@tag2),@grp,@cap,convert(int,@pgsize)) 
end 
end 
if (@grp>'') delete mps_ext_resource2 where etc2_etcid=@objid and etc2_tag=convert(int,@tag2) and etc2_group='' 
end
if (@objtype='prd') 
begin
if (exists (select * from mps_prdctg2 where pc2_pcid=@objid and pc2_tag=convert(int,@tag2) and pc2_group=@grp)) 
begin
update mps_prdctg2 set pc2_caption=@cap , pc2_page=convert(int,@pgsize) where pc2_pcid=@objid and pc2_tag=convert(int,@tag2) and pc2_group=@grp 
end else 
begin
if (exists (select * from mps_prdctg2 where pc2_pcid=@objid and pc2_tag=convert(int,@tag2) and pc2_group='')) 
begin
update mps_prdctg2 set pc2_caption=@cap , pc2_group=@grp , pc2_page=convert(int,@pgsize) where pc2_pcid=@objid and pc2_tag=convert(int,@tag2) and pc2_group=''  
end else 
begin
insert into mps_prdctg2(pc2_pcid,pc2_tag,pc2_group,pc2_caption,pc2_page) 
values(@objid,convert(int,@tag2),@grp,@cap,convert(int,@pgsize)) 
end 
end 
if (@grp>'') delete mps_prdctg2 where pc2_pcid=@objid and pc2_tag=convert(int,@tag2) and pc2_group='' 
end
if (@objtype='equ') 
begin
if (exists (select * from mps_equctg2 where ec2_ecid=@objid and ec2_tag=convert(int,@tag2) and ec2_group=@grp)) 
begin
update mps_equctg2 set ec2_caption=@cap , ec2_page=convert(int,@pgsize) where ec2_ecid=@objid and ec2_tag=convert(int,@tag2) and ec2_group=@grp 
end else 
begin
if (exists (select * from mps_equctg2 where ec2_ecid=@objid and ec2_tag=convert(int,@tag2) and ec2_group='')) 
begin
update mps_equctg2 set ec2_caption=@cap , ec2_group=@grp , ec2_page=convert(int,@pgsize) where ec2_ecid=@objid and ec2_tag=convert(int,@tag2) and ec2_group=''  
end else 
begin
insert into mps_equctg2(ec2_ecid,ec2_tag,ec2_group,ec2_caption,ec2_page) 
values(@objid,convert(int,@tag2),@grp,@cap,convert(int,@pgsize)) 
end 
end 
if (@grp>'') delete mps_equctg2 where ec2_ecid=@objid and ec2_tag=convert(int,@tag2) and ec2_group='' 
end
if (@objtype='prg' or @objtype='usr') 
begin
if (exists (select * from mps_programtype2 where pt2_ptid=@objid and pt2_tag=convert(int,@tag2) and pt2_group=@grp)) 
begin
update mps_programtype2 set pt2_caption=@cap , pt2_page=convert(int,@pgsize) where pt2_ptid=@objid and pt2_tag=convert(int,@tag2) and pt2_group=@grp 
end else 
begin
if (exists (select * from mps_programtype2 where pt2_ptid=@objid and pt2_tag=convert(int,@tag2) and pt2_group='')) 
begin
update mps_programtype2 set pt2_caption=@cap , pt2_group=@grp , pt2_page=convert(int,@pgsize) where pt2_ptid=@objid and pt2_tag=convert(int,@tag2) and pt2_group=''  
end else 
begin
insert into mps_programtype2(pt2_ptid,pt2_tag,pt2_group,pt2_caption,pt2_page) 
values(@objid,convert(int,@tag2),@grp,@cap,convert(int,@pgsize)) 
end 
end 
if (@grp>'') delete mps_programtype2 where pt2_ptid=@objid and pt2_tag=convert(int,@tag2) and pt2_group='' 
end
end 
end