IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_delete_1field_byid]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_delete_1field_byid]
GO
 
CREATE procedure [dbo].[mps_delete_1field_byid] 
@fldid int 
as
begin
declare @fldid2 int 

declare @cmpid int 
declare @tbtype varchar(10) 
declare @objid int  
declare @objid2 int  
declare @fldname varchar(50) 

    select top 1 @cmpid=fldi_cmpid, @tbtype=fldi_tbtype,@objid=fldi_tbid,@objid2=fldi_ord2tag , @fldname=fldi_fieldname  from mps_fieldinfo where fldi_id=@fldid 
select top 1 @fldid2 = 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=@fldname  
set @fldid2 = isnull(@fldid2,0) 

if (@fldid2=0)
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_fieldname_old,fldi_datatype_old,fldi_size_old,fldi_decimal_old,fldi_default_old) 
select @cmpid , @tbtype , @objid , @objid2 ,
'' , '',0 , 0 ,'' ,
@fldname,'',0,0,''  
end else 
begin
update mps_fieldinfo_operation set 
fldi_size = 0 ,
fldi_decimal = 0 , 
fldi_default = '' ,
fldi_datatype = '' ,
fldi_fieldname = ''  
where fldi_id=@fldid2 
end 


update mps_fieldinfo set 
fldi_oldname=fldi_fieldname , 
fldi_fieldname = '' , 
fldi_changed = 0 
where fldi_id=@fldid  

end