IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_biz_comefrom]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_biz_comefrom]
GO
 
CREATE procedure [dbo].[mps_g_biz_comefrom] 
@cmpid int ,
@bizid  int ,
@stype varchar(10),
@sbizid int ,
@sord2tag int ,
@sord2group nvarchar(50)  
as
begin
set nocount on
-- 首先返回主业务中有多少字段是需要配置的。
declare @bsid int 
declare @tord2tag int 
declare @newbyord2 int
select top 1 @bsid =bs_id ,@tord2tag=bs_ord2tag,@newbyord2=bs_newviaord2  from mps_biztype_comefrom 
where bs_bizid=@bizid and   bs_stype=@stype and bs_sbizid=@sbizid and bs_sord2tag=@sord2tag   

-- 是主表
if (@tord2tag=0) 
begin
select fldi_id,fldi_tbtype,fldi_tbid,fldi_ord2tag,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,
case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize  ,
case when ISNULL(bsf_id,0)=0 then 1 else 0 end  as bsf_id 
from mps_biztype_comefrom_fieldmap RIGHT OUTER JOIN mps_fieldinfo ON bsf_tfldname=fldi_fieldname and bsf_bsid=@bsid 
where fldi_cmpid=@cmpid and fldi_tbtype='biz' and fldi_tbid=@bizid and fldi_ord2tag=0  
order by case when ISNULL(bsf_id,0)=0 then 1 else 0 end, fldi_system desc ,fldi_datatype,fldi_datatypesize,fldi_fieldcaption 
end else 
begin   -- 可能是明细表中的字段
select fldi_id,fldi_tbtype,fldi_tbid,fldi_ord2tag,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,
case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize  ,
case when ISNULL(bsf_id,0)=0 then 1 else 0 end  as bsf_id 
from mps_biztype_comefrom_fieldmap RIGHT OUTER JOIN mps_fieldinfo ON bsf_tfldname=fldi_fieldname and bsf_bsid=@bsid  
where fldi_cmpid=@cmpid and fldi_tbtype='biz' and fldi_tbid=@bizid and fldi_ord2tag=@tord2tag    
order by case when ISNULL(bsf_id,0)=0 then 1 else 0 end, fldi_system desc ,fldi_datatype,fldi_datatypesize,fldi_fieldcaption 
end 

-- 返回哪些字段已经被选择了
select a.* from mps_biztype_comefrom_fieldmap a 
where bsf_bsid=@bsid 

-- 返回源所有字段
if (@sord2tag<=0)  
begin
select fldi_id,fldi_tbtype,fldi_tbid,fldi_ord2tag,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,
case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize  
from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype=@stype and fldi_tbid=@sbizid and fldi_ord2tag=0  
order by fldi_system desc ,fldi_datatype,fldi_datatypesize,fldi_fieldcaption 
select 0 none where 1=0
end else 
begin
if (@newbyord2=1)  -- 不需要按明细表建单 
begin
--select 0 none where 1=0
select fldi_id,fldi_tbtype,fldi_tbid,fldi_ord2tag,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,
case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize  
from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype='biz' and fldi_tbid=@sbizid and fldi_ord2tag=0  
order by fldi_system desc ,fldi_datatype,fldi_datatypesize,fldi_fieldcaption 
select fldi_id,fldi_tbtype,fldi_tbid,fldi_ord2tag,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,
case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize  
from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype='biz' and fldi_tbid=@sbizid and fldi_ord2tag=@sord2tag  
order by fldi_system desc ,fldi_datatype,fldi_datatypesize,fldi_fieldcaption 
end else 
begin
select fldi_id,fldi_tbtype,fldi_tbid,fldi_ord2tag,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,
case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize  
from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype='biz' and fldi_tbid=@sbizid and fldi_ord2tag=0  
order by fldi_system desc ,fldi_datatype,fldi_datatypesize,fldi_fieldcaption 
select fldi_id,fldi_tbtype,fldi_tbid,fldi_ord2tag,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,
case when fldi_datatype='string' then 'str('+ltrim(rtrim(str(fldi_size)))+')' else fldi_datatype end fldi_datatypesize   
from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype='biz' and fldi_tbid=@sbizid and fldi_ord2tag=@sord2tag  
order by fldi_system desc ,fldi_datatype,fldi_datatypesize,fldi_fieldcaption 
end 
end 
-- 返回主表的明细源
select distinct biz2_bizid,biz2_tag,biz2_caption biz2_sourcename 
from mps_biztype2 where biz2_bizid=@bizid  and  biz2_tag>0   
-- 返回bs 
select * from mps_biztype_comefrom where bs_id=@bsid 
set nocount off
end