IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_branch]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_branch]
GO
  
 CREATE procedure [dbo].[mps_new_branch] 
  
 @cmpid int ,
  
 @bc_code varchar(30),
  
 @bc_company nvarchar(100) 
  
 as
  
 begin
  
 declare @bcid int 
 declare @neworder int
  
 select @bcid = bc_id from mps_branch where bc_cmpid=@cmpid and bc_code=@bc_code 
  
 set @bcid = isnull(@bcid , 0) 
  
 if (@bcid=0) 
  
 begin
  
select @neworder = max(bc_order) from mps_branch where bc_cmpid=@cmpid

  insert into mps_branch(bc_cmpid,bc_code , bc_company,bc_order) 
  
  values(@cmpid ,@bc_code , @bc_company,@neworder+1) 
  
  select * from mps_branch where bc_cmpid=@cmpid and bc_code=@bc_code 
  
 end else 
  
 begin
  
 select 1 none where 0=1
  
 end 
  
 end