IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_tb_field_info]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_tb_field_info]
GO
CREATE procedure [dbo].[mps_g_tb_field_info](
@cmpid int ,
@biztype varchar(50),
@bizid int,
@ord2tag int ,
@forsystem int = default 
) as
begin
set nocount on

declare @system int 
set @system = @forsystem 
if (@ord2tag>10000) set @ord2tag=0 
if (@ord2tag=-999999999) set @ord2tag=0 
set @system = isnull(@system , -1) 
if (@system=-999999999) set @system=-1 
declare @biztype1 varchar(50) 
declare @ftag varchar(10) 
declare @i int 
declare @t table(biztype varchar(10),bizid int, ord2tag int , level int , mybizid int) 
declare @tt table(bid int) 
declare @fld table(fldname varchar(50),fldtype varchar(30),fldsize int ,count1 int , fldid int) 
declare @count int
set @biztype1 = @biztype 
set @i = charindex('_',@biztype1) 
if (@i>0) 
begin
set @ftag = substring(@biztype1,@i+1,len(@biztype1)-@i  ) 
set @biztype1 = substring(@biztype1,1,@i - 1) 
end else 
begin
set @ftag='' 
end

if (@bizid>100000000 and @bizid<500000000)
begin
set @bizid = @bizid - (@bizid/100000000)*100000000
end else 
if (@bizid>500000000)
begin
set @bizid = @bizid - (@bizid/100000000)*100000000
if (@biztype='cst') select top 1 @bizid=ct_tag from mps_csttype where ct_id=@bizid 
if (@biztype='prd') select top 1 @bizid=pc_tag from mps_prdctg where pc_id=@bizid 
if (@biztype='equ') select top 1 @bizid=ec_tag from mps_equctg where ec_id=@bizid 
if (@biztype='etc') select top 1 @bizid=etc_tag from mps_ext_resource where etc_id=@bizid 
end 
 
 

-- @ftag 有以下几种情况：f ff r (上级 上上级 根级)
if (@ftag='') 
begin
if (@biztype1='emp') begin set @bizid=0  set @ord2tag=0 end 
if (@biztype1='doc') begin set @bizid=0  set @ord2tag=0 end 
if (@biztype1='dpt') begin set @bizid=0  set @ord2tag=0 end 
if (@biztype1='shp') begin set @bizid=0  set @ord2tag=0 end 
if (@biztype1='brch') begin set @bizid=0  set @ord2tag=0 end 
select * , case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize  
from mps_fieldinfo 
where fldi_tbtype=@biztype1 and fldi_tbid=@bizid and fldi_ord2tag=@ord2tag  and 
fldi_system = case when @system<0 then fldi_system else @system end 
order by fldi_fieldname 
end else
if (@ftag='f' and @biztype1='biz') 
begin
-- 生成直接的源（父级）
insert into @t(biztype,bizid,ord2tag,level) 
select bs_stype , bs_sbizid , bs_sord2tag , 0 
from mps_biztype_comefrom
where bs_bizid=@bizid and bs_ord2tag=@ord2tag and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 ))

select @count=count(*) from @t 
-- 找到的具有共同特性的字段
insert into @fld(fldname,fldtype,fldsize,count1 , fldid) 
select fldi_fieldname ,fldi_datatype,fldi_size, count(fldi_id),min(fldi_id) from mps_fieldinfo , @t 
where fldi_tbtype=biztype and fldi_tbid=bizid and fldi_ord2tag=ord2tag  and 
fldi_system = case when @system<0 then fldi_system else @system end 
group by fldi_fieldname ,fldi_datatype,fldi_size 
delete @fld where count1<>@count 
select a.* , case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize  
from mps_fieldinfo a , @fld  
where fldi_id=fldid 
order by fldi_fieldname 
end  else
if (@ftag='ff' and @biztype1='biz') 
begin
insert into @t(biztype,bizid,ord2tag , level) 
select bs_stype , bs_sbizid , bs_sord2tag , 0 
from mps_biztype_comefrom
where bs_bizid=@bizid and bs_ord2tag=@ord2tag and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 ))

-- 寻找父级的上级
insert into @t(biztype,bizid,ord2tag,level) 
select bs_stype , bs_sbizid , bs_sord2tag , 1 
from mps_biztype_comefrom , @t 
where bs_bizid=bizid and bs_ord2tag=ord2tag and biztype='biz' and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 )) 

delete @t where level=0 

select @count=count(*) from @t 
-- 找到的具有共同特性的字段
insert into @fld(fldname,fldtype,fldsize,count1 , fldid) 
select fldi_fieldname ,fldi_datatype,fldi_size, count(fldi_id),min(fldi_id) from mps_fieldinfo , @t 
where fldi_tbtype=biztype and fldi_tbid=bizid and fldi_ord2tag=ord2tag  and 
fldi_system = case when @system<0 then fldi_system else @system end 
group by fldi_fieldname ,fldi_datatype,fldi_size 
delete @fld where count1<>@count 
select a.* , case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize  
from mps_fieldinfo a , @fld  
where fldi_id=fldid 
order by fldi_fieldname 
end   else
if (@ftag='r' and @biztype1='biz') 
begin
insert into @t(biztype,bizid,ord2tag , level ,mybizid) 
select bs_stype , bs_sbizid , bs_sord2tag , 0 , 0 
from mps_biztype_comefrom
where bs_bizid=@bizid and bs_ord2tag=@ord2tag and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 ))

-- 寻找父级的上级
insert into @t(biztype,bizid,ord2tag,level,mybizid) 
select bs_stype , bs_sbizid , bs_sord2tag , 1 , bizid 
from mps_biztype_comefrom , @t 
where bs_bizid=bizid and bs_ord2tag=ord2tag and biztype='biz' and level=0 and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 )) 

delete @tt 
insert into @tt(bid) select mybizid from @t where level=1 
delete @t from @tt where level=0 and bizid=bid 

-- 寻找父级的上级
insert into @t(biztype,bizid,ord2tag,level,mybizid) 
select bs_stype , bs_sbizid , bs_sord2tag , 2 , bizid 
from mps_biztype_comefrom , @t 
where bs_bizid=bizid and bs_ord2tag=ord2tag and biztype='biz' and level=1 and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 )) 

delete @tt 
insert into @tt(bid) select mybizid from @t where level=2 
delete @t from @tt where level=1 and bizid=bid 

-- 寻找父级的上级
insert into @t(biztype,bizid,ord2tag,level,mybizid) 
select bs_stype , bs_sbizid , bs_sord2tag , 3 , bizid 
from mps_biztype_comefrom , @t 
where bs_bizid=bizid and bs_ord2tag=ord2tag and biztype='biz' and level=2 and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 )) 

delete @tt 
insert into @tt(bid) select mybizid from @t where level=3 
delete @t from @tt where level=2 and bizid=bid 
-- 寻找父级的上级
insert into @t(biztype,bizid,ord2tag,level,mybizid) 
select bs_stype , bs_sbizid , bs_sord2tag , 4 , bizid 
from mps_biztype_comefrom , @t 
where bs_bizid=bizid and bs_ord2tag=ord2tag and biztype='biz' and level=3 and 
(exists (select * from mps_biztype_comefrom_fieldmap where bsf_bsid=bs_id and bsf_use=0 )) 

delete @tt 
insert into @tt(bid) select mybizid from @t where level=4 
delete @t from @tt where level=3 and bizid=bid 

delete @t where level=0

select @count=count(*) from @t 
-- 找到的具有共同特性的字段
insert into @fld(fldname,fldtype,fldsize,count1 , fldid) 
select fldi_fieldname ,fldi_datatype,fldi_size, count(fldi_id),min(fldi_id) from mps_fieldinfo , @t 
where fldi_tbtype=biztype and fldi_tbid=bizid and fldi_ord2tag=ord2tag  and 
fldi_system = case when @system<0 then fldi_system else @system end 
group by fldi_fieldname ,fldi_datatype,fldi_size 
delete @fld where count1<>@count 
select a.* , case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize  
from mps_fieldinfo a , @fld  
where fldi_id=fldid 
order by fldi_fieldname 
end 
set nocount off
end