IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_fieldinfo_maintain]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_fieldinfo_maintain]
GO
create procedure [dbo].[mps_g_fieldinfo_maintain](
@cmpid int,
@tbtype varchar(20),
@tbid int , 
@ord2tag int 
)
as
begin
set nocount on
declare @dbname varchar(50)
declare @tbname varchar(50) 
declare @keyfld varchar(50) 
declare @tag int  
set @tag = @tbid  
if (@tbtype='cst' or @tbtype='prd' or @tbtype='equ') 
begin
if (@tbtype='cst') 
begin
select @tag = ct_tag from mps_csttype where ct_id=@tbid 
end else 
if (@tbtype='prd') 
begin
select @tag = pc_tag from mps_prdctg where pc_id=@tbid 
end else 
if (@tbtype='equ') 
begin
select @tag = ec_tag from mps_equctg where ec_id=@tbid 
end 
end 
exec sys_sql_getTableNamePk @cmpid,@tbtype,@tbid,@ord2tag,@tbname output , @keyfld output , @dbname output 
select *,
case when (select COUNT(1) from ebs_kernel..pub_fieldinfo_standard a
 where a.fldi_tbtype=@tbtype and a.fldi_fieldname=b.fldi_fieldname and  a.fldi_ord2tag=(case when @ord2tag=0 then 1 else 0 end)
 )=0 then 0 else 1 end  fldi_issystemfield 
from mps_fieldinfo b
where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_fieldname>'' and 
fldi_tbid=@tag  and fldi_ord2tag=@ord2tag  
order by fldi_issystemfield   , fldi_fieldname  
    
    if ((@tbtype='biz' or @tbtype='cst' or @tbtype='prd' or @tbtype='equ' or @tbtype='usr')  and @ord2tag=0) 
    begin
if (@tbtype='biz') 
begin
select biz2_bizid ordtag,biz2_tag ord2tag , isnull(biz2_caption,'明细表'+CONVERT(varchar(10),biz2_tag)) ord2caption from mps_biztype2 where biz2_bizid=@tbid and biz2_caption>''
end else 
if (@tbtype='cst') 
begin
select ct2_ctid ordtag,ct2_tag  ord2tag , isnull(ct2_caption,'明细表'+CONVERT(varchar(10),ct2_tag)) ord2caption from mps_csttype2 where ct2_ctid=@tbid  and ct2_caption>''
end  else 
begin
select 0 none , 1 none1 where 0=1 
end 
    end else 
    begin
select 0 none , 1 none1 where 0=1 
    end 
set nocount off 
end