IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_template]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_template]
GO
CREATE procedure [dbo].[mps_new_template] 
@cmpid int,
@tptype varchar(10),
@objid int,
@objid2 int,
@tpcaption nvarchar(100)
 as
begin
set nocount on
if (@tpcaption<>'_none_' and @tpcaption<>'') 
begin
declare @tmpid int 
select top 1 @tmpid=tmp_id from mps_template 
where tmp_cmpid=@cmpid and tmp_type=@tptype  and tmp_objid=@objid and tmp_objid2=@objid2   and tmp_caption=@tpcaption 
set @tmpid = isnull(@tmpid,0) 
if (@tmpid=0) 
begin
insert into mps_template(tmp_cmpid,tmp_type,tmp_objid,tmp_objid2 ,tmp_caption)
values(@cmpid ,@tptype ,@objid,@objid2 ,@tpcaption)  
end 
end 
select tmp_caption tmp_display , tmp_caption tmp_return , * from mps_template 
where tmp_cmpid=@cmpid  and tmp_type=@tptype and tmp_objid=@objid  and tmp_objid2=@objid2   
order by tmp_id 
set nocount off 
end