IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_source_both_fields]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_source_both_fields]
GO
CREATE procedure [dbo].[mps_g_source_both_fields](
@cmpid int ,
@objtype varchar(20),
@objid int,
@objid2 int ,
@sourcename nvarchar(100) ,
@fldss varchar(2000)=default
) as
begin
set nocount on
if (@fldss is null or @fldss='_none_') set @fldss='' 
declare @tfld table(fldi_fieldname varchar(50),fldi_fieldcaption nvarchar(50),fldi_datatype varchar(20),fldi_size int) 
declare @i int 
declare @s1 varchar(3000) 
declare @extflds int 
set @extflds = 1 
while (@fldss>'') 
begin
set @extflds = 0 
set @i = CHARINDEX(',',@fldss) 
if (@i>0)
begin
set @s1 = SUBSTRING(@fldss,1,@i-1) 
set @fldss = SUBSTRING(@fldss,@i+1,len(@fldss)-@i) 
end else
begin
set @s1 = @fldss
set @fldss = ''
end 
if (@s1>'') 
insert into @tfld(fldi_fieldname ,fldi_fieldcaption ,fldi_datatype ,fldi_size)
values(@s1,@s1,'varchar',50) 
end 
declare @obj2 int 
if (@objtype='emp' or @objtype='dpt' or @objtype='doc' or @objtype='brch' or @objtype='shp' or @objtype='fee' or @objtype='barn') 
begin
set @obj2=0  
end else 
begin
set @obj2=@objid2 
end
declare @obj1 int 
if (@objtype='emp' or @objtype='dpt' or @objtype='doc' or @objtype='brch' or @objtype='shp' or @objtype='fee' or @objtype='barn') 
begin
set @obj1=0  
end else 
begin
set @obj1=@objid 
end

declare @tag int 
set @tag = @objid 
 
if (@objtype='cst') 
begin
select top 1 @tag=ct_tag from mps_csttype where ct_id=@objid 
end else 
if (@objtype='prd') 
begin
select top 1 @tag=pc_tag from mps_prdctg where pc_id=@objid 
end else 
if (@objtype='equ') 
begin
select top 1 @tag=ec_tag from mps_equctg where ec_id=@objid 
end  else 
if (@objtype='etc') 
begin
select top 1 @tag=etc_tag from mps_ext_resource where etc_id=@objid 
end 

/*
select a.* from mps_fieldinfo a, mps_biztype_source b  
where bizs_type=@objtype and bizs_bizid=@objid and bizs_ord2tag=@objid2 and bizs_source=@sourcename and 
fldi_cmpid=@cmpid and fldi_tbtype=bizs_type and fldi_tbid=@obj1 and fldi_ord2tag=@obj2 
order by case when charindex('old.'+fldi_fieldname,bizs_insertsql)>0 then 0 else 1 end , fldi_fieldname 
*/

if (@extflds=0) 
select * from @tfld order by fldi_fieldname 
else 
select a.* from mps_fieldinfo a , mps_biztype_source b 
where  bizs_type=@objtype and bizs_bizid=@objid and bizs_ord2tag=@objid2 and bizs_source=@sourcename and 
fldi_cmpid=@cmpid and fldi_tbtype=@objtype and fldi_tbid=@tag and fldi_ord2tag=@obj2 
order by case when charindex('old.'+fldi_fieldname,bizs_insertsql)>0 then 0 else 1 end , fldi_fieldname 

select a.* from mps_fieldinfo a, mps_biztype_source b   
where bizs_type=@objtype and bizs_bizid=@objid and bizs_ord2tag=@objid2 and bizs_source=@sourcename and 
fldi_cmpid=@cmpid and fldi_tbtype=bizs_sourcetype and 
fldi_tbid=case when bizs_sourcetype='emp' then 0 
when bizs_sourcetype='doc' then 0 
when bizs_sourcetype='dpt' then 0 
when bizs_sourcetype='brch' then 0 
when bizs_sourcetype='shp' then 0 
when bizs_sourcetype='barn' then 0 
when bizs_sourcetype='fee' then 0 
when bizs_sourcetype='cst' and (bizs_ctgid/100000000)<=4 
then bizs_ctgid - (bizs_ctgid/100000000) * 100000000 
when bizs_sourcetype='cst' and (bizs_ctgid/100000000)>=5  
then (select top 1 ct_tag from mps_csttype where ct_id=(bizs_ctgid - (bizs_ctgid/100000000) * 100000000))  
when bizs_sourcetype='prd' and (bizs_ctgid/100000000)<=4 
then bizs_ctgid - (bizs_ctgid/100000000) * 100000000 
when bizs_sourcetype='prd' and (bizs_ctgid/100000000)>=5  
then (select top 1 pc_tag from mps_prdctg where pc_id=(bizs_ctgid - (bizs_ctgid/100000000) * 100000000))  
when bizs_sourcetype='equ' and (bizs_ctgid/100000000)<=4 
then bizs_ctgid - (bizs_ctgid/100000000) * 100000000 
when bizs_sourcetype='equ' and (bizs_ctgid/100000000)>=5  
then (select top 1 ec_tag from mps_equctg where ec_id=(bizs_ctgid - (bizs_ctgid/100000000) * 100000000))  
when bizs_sourcetype='etc' and (bizs_ctgid/100000000)<=4 
then bizs_ctgid - (bizs_ctgid/100000000) * 100000000 
when bizs_sourcetype='etc' and (bizs_ctgid/100000000)>=5  
then (select top 1 etc_tag from mps_ext_resource where etc_id=(bizs_ctgid - (bizs_ctgid/100000000) * 100000000))  
else bizs_ctgid 
end and 
fldi_ord2tag=
case when bizs_sourcetype='emp' then 0 
when bizs_sourcetype='doc' then 0 
when bizs_sourcetype='dpt' then 0 
when bizs_sourcetype='fee' then 0 
when bizs_sourcetype='barn' then 0 
when bizs_sourcetype='brch' then 0
when bizs_sourcetype='shp' then 0  
else bizs_ctg2id 
end 
order by fldi_fieldname 
set nocount off
end