IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_field_infos_nosys]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_field_infos_nosys]
GO
CREATE procedure [dbo].[mps_g_field_infos_nosys]
(
@cmpid int ,
@objtype varchar(10),
@objid int ,
@objid2 int
) as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

if (@objtype='cst') 
begin
select top 1 @objid=ct_tag from mps_csttype  with(nolock) where ct_id=@objid 
end else 
if (@objtype='prd') 
begin
select top 1 @objid=pc_tag from mps_prdctg  with(nolock) where pc_id=@objid 
end else 
if (@objtype='equ') 
begin
select top 1 @objid=ec_tag from mps_equctg  with(nolock) where ec_id=@objid 
end  

if (@objid2=0) 
begin
if (@objtype='emp') 
select  * from mps_fieldinfo  with(nolock)   where fldi_cmpid=@cmpid and fldi_tbtype=@objtype  and fldi_system=1 and fldi_hidden=1 order by fldi_fieldname 
else 
select  * from mps_fieldinfo  with(nolock)   where fldi_cmpid=@cmpid and fldi_tbtype=@objtype and fldi_tbid=@objid and fldi_ord2tag=@objid2  and fldi_system=1 and fldi_hidden=1 order by fldi_fieldname 
end else 
begin
if (@objtype='emp') 
begin
select  * from mps_fieldinfo   with(nolock)  where fldi_cmpid=@cmpid and (fldi_tbtype=@objtype or fldi_tbtype=@objtype+ltrim(rtrim(str(@objid2))))  and fldi_system=1 and fldi_hidden=1 
union 
select  * from mps_fieldinfo  with(nolock)   where fldi_cmpid=@cmpid and fldi_tbtype=@objtype and  fldi_system=1 and fldi_hidden=1 
order by fldi_ord2tag desc , fldi_fieldname
end else 
begin
select  * from mps_fieldinfo   with(nolock)  where fldi_cmpid=@cmpid and (fldi_tbtype=@objtype or fldi_tbtype=@objtype+ltrim(rtrim(str(@objid2)))) and fldi_tbid=@objid and fldi_ord2tag=@objid2  and fldi_system=1 and fldi_hidden=1 
union 
select  * from mps_fieldinfo  with(nolock)   where fldi_cmpid=@cmpid and fldi_tbtype=@objtype and fldi_tbid=@objid and fldi_ord2tag=0  and fldi_system=1 and fldi_hidden=1 
order by fldi_ord2tag desc , fldi_fieldname
end 
end 
set nocount off
end