-- Collection of code snippets by Arne Vajhøj 
-- (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
CREATE PROCEDURE UPDWITHHIST
@f1 INTEGER,
@f2 INTEGER
As

DECLARE @oldf2 INTEGER

SET @oldf2 = (SELECT f2 FROM currentdata WHERE f1=@f1)

IF @oldf2 IS NULL
BEGIN
    INSERT INTO currentdata VALUES(@f1,@f2)
END
ELSE
BEGIN
   INSERT INTO historydata VALUES(@f1,@oldf2)
   UPDATE currentdata SET f2 = @f2 WHERE f1=@f1
END

GO
