IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_fieldsinfo]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_fieldsinfo]
GO

CREATE procedure [dbo].[mps_new_fieldsinfo] 
@cmpid int ,
@tbtype varchar(10) ,
@objid int ,
@objid2 int ,
@fldi_fieldname varchar(50) , 
@fldi_fieldcaption nvarchar(100) ,
@fldi_datatype varchar(20) ,
@fldi_size int ,
@fldi_decimal int ,
@fldi_default nvarchar(100) 
as
begin

declare @dbname varchar(30) 
declare @tbname varchar(50) 
declare @pkname varchar(50) 

declare @objid_ int 
set @objid_ = @objid * (-1) 
if (@tbtype='cst' or @tbtype='equ' or @tbtype='prd' or @tbtype='etc') 
exec sys_sql_getTableNamePk @cmpid , @tbtype,@objid_,@objid2,@tbname output , @pkname output ,@dbname output 
else exec sys_sql_getTableNamePk @cmpid , @tbtype,@objid,@objid2,@tbname output , @pkname output ,@dbname output 

if (@fldi_datatype='string') set @fldi_datatype='nvarchar' 

set @dbname = replace(@dbname , '.dbo.' , '') 

declare @fldid int 

select top 1 @fldid = fldi_id from mps_fieldinfo_operation where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2 and fldi_fieldname=@fldi_fieldname  
set @fldid = isnull(@fldid,0) 
if (@fldid=0)
begin
   if (not exists (select * from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2 and fldi_fieldname=@fldi_fieldname))
   begin
insert into mps_fieldinfo_operation(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag,
fldi_database,fldi_tbname,fldi_key,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default,
fldi_fieldname_old,fldi_datatype_old,fldi_size_old,fldi_decimal_old,fldi_default_old) 
select @cmpid , @tbtype , @objid , @objid2 ,
@dbname,@tbname,@pkname,
@fldi_fieldname , @fldi_datatype,
@fldi_size ,@fldi_decimal , 
@fldi_default ,'','',0,0,'' 
end else 
begin
insert into mps_fieldinfo_operation(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag,fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default,
fldi_database,fldi_tbname,fldi_key,
fldi_fieldname_old,fldi_datatype_old,fldi_size_old,fldi_decimal_old,fldi_default_old) 
select @cmpid , @tbtype , @objid , @objid2 ,
@dbname,@tbname,@pkname,
@fldi_fieldname , @fldi_datatype,
@fldi_size , @fldi_decimal ,
@fldi_default ,
fldi_fieldname,fldi_datatype,fldi_size,fldi_decimal,fldi_default 
from mps_fieldinfo 
where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2 and fldi_fieldname=@fldi_fieldname 
end 
end else 
begin
update mps_fieldinfo_operation set 
fldi_size = @fldi_size ,
fldi_decimal = @fldi_decimal   , 
fldi_default = @fldi_default ,
fldi_datatype = @fldi_datatype 
where fldi_id=@fldid 
end 

select top 1 @fldid = fldi_id from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2 and fldi_fieldname=@fldi_fieldname  
set @fldid = isnull(@fldid,0) 
if (@fldid=0)
begin
insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_ord2tag,
fldi_database,fldi_tbname,fldi_key,fldi_fieldname,fldi_fieldcaption,fldi_datatype,fldi_size,fldi_decimal,fldi_default) 
select @cmpid , @tbtype , @objid , @objid2 ,
@dbname,@tbname,@pkname,
@fldi_fieldname , @fldi_fieldcaption,@fldi_datatype,
@fldi_size , @fldi_decimal ,
@fldi_default  
select * , 0 fldi_issystemfield  from mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2 and fldi_fieldname=@fldi_fieldname  
end else 
begin
update mps_fieldinfo set 
fldi_fieldcaption = @fldi_fieldcaption ,
fldi_size =   @fldi_size ,
fldi_decimal = @fldi_decimal , 
fldi_default = @fldi_default ,
fldi_datatype = @fldi_datatype , 
fldi_changed = 0 
where fldi_id=@fldid  
select 1 none where 1=0 
end 
end