IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_newcodemanagement]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_newcodemanagement]
GO
CREATE procedure [dbo].[mps_newcodemanagement]   
@cmpid int ,  
@ctg varchar(20) ,  
@smallctg nvarchar(50) ,
@smallctgen nvarchar(50) 
as  
begin  
set nocount on    
declare @bmid int    
 select top 1 @bmid = bm_id from mps_code   
 where bm_cmpid=@cmpid and bm_category=@ctg and bm_ctgcaption=@smallctg  
 set @bmid = isnull(@bmid,0)   
 if (@bmid=0)  
 begin  
       insert into mps_code(bm_cmpid,bm_category,bm_ctg,bm_ctgcaption,bm_text,bm_order)  
       values(@cmpid,@ctg,@smallctgen,@smallctg,@smallctg,1)      
       select top 1 * from mps_code where bm_cmpid=@cmpid and bm_category=@ctg and bm_ctgcaption=@smallctg order by bm_id desc   
   end else   
    begin   
     select 0 none where 0=1   
    end   
    set nocount off   
end