create procedure sp_increase
@tmpid     int,
@tmpfield  varchar(100)
as
begin
    declare @t int
    select @t=isnull(count(id),0) from table1 where tmpid=@tmpid and tmpfield=@tmpfield
    
    set rowcount 1
    
    
    if exists(select 1 from table2 where tmpid=@tmpid)
        update table2 
        set 
            c1=c1+1,
            c2=c2+(case @t when 0 then 1 else 0 end) 
        where 
            tmpid=@tmpid
    else
        insert into table2(tmpid,c1,c2) values(@tmpid,1,1)  
    
    set rowcount 0
end
go