IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_bizcomefrom_1]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_bizcomefrom_1]
GO
CREATE procedure [dbo].[mps_save_bizcomefrom_1]
@bsid int ,
@bsf_tfldname nvarchar(50),
@bsf_expression nvarchar(1000),
@bsf_method int ,
@bsf_datatype varchar(20)
as
begin
if (@bsf_tfldname='_none_' or @bsf_tfldname='' or len(@bsf_tfldname)<=2) return 
if (@bsf_expression='_none_' or @bsf_expression='')  
begin
delete  mps_biztype_comefrom_fieldmap where  bsf_bsid=@bsid  and bsf_tfldname=@bsf_tfldname  
end else 
   begin
   declare @bsfid int 
select top 1 @bsfid=bsf_id from mps_biztype_comefrom_fieldmap where bsf_bsid=@bsid and bsf_tfldname=@bsf_tfldname  
set @bsfid = isnull(@bsfid,0) 
if (@bsfid>0)
begin
update mps_biztype_comefrom_fieldmap
set 
bsf_use=0 ,
bsf_expression=case when @bsf_expression='_none_' then bsf_expression else @bsf_expression end ,
bsf_method=case when @bsf_method=-999999999 then bsf_method else @bsf_method end ,
bsf_datatype=case when @bsf_datatype='_none_' then bsf_datatype else @bsf_datatype end 
where bsf_id=@bsfid 
end else 
begin
insert into mps_biztype_comefrom_fieldmap(bsf_bsid,bsf_tfldname,bsf_expression,bsf_method,bsf_datatype) 
values(@bsid,@bsf_tfldname,
case when @bsf_expression='_none_' then '' else @bsf_expression end ,
case when @bsf_method=-999999999 then 0 else @bsf_method end ,
case when @bsf_datatype='_none_' then '' else @bsf_datatype end) 
end 
   end 
 end