Not sure how elegant one can get, but we might do it just the way you describe. Query the database
select Value from Tab1 where Type=@type and Number=@num
and if no rows are returned, compute the value, then store it in the database for next time.
However, if the "compute the value" requires the database itself, and we can compute it in the database, then we can do the whole cycle with one database round trip -- more 'elegant' perhaps but faster at least than 3 round trips (lookup, compute, store).
declare @val int
select @val=Value from Tab1 where Type=@type and Number=@num
if @@ROWCOUNT=0 BEGIN
exec compute_val @type,@num,@val OUTPUT
insert into Tab1 values (@type,@num,@val)
END
SELECT @val[Value]--return
The only use for SQL Merge is if you think there may be concurrent users and the number is inserted between above select and insert, giving an error on the insert. I'd just catch the error and skip the insert (as we can assume the value won't be different by definition).