IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_delete_1field]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_delete_1field]
GO
CREATE procedure [dbo].[mps_delete_1field] 
@cmpid int ,
@tbtype varchar(10) ,
@objid int ,
@objid2 int ,
@fldname varchar(50) 
as
begin
declare @fldid int 

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

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=@fldname  
set @fldid = isnull(@fldid,0) 
if (@fldid=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=@fldid 
end 

delete  mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype=@tbtype and fldi_tbid=@objid and fldi_ord2tag=@objid2 and fldi_fieldname=@fldname  

end