-- 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 TABLE noget (
    id INTEGER IDENTITY PRIMARY KEY,
    sameasid INTEGER,
    code INTEGER
)
GO
CREATE PROCEDURE usp_pairinsert
@v1 INTEGER,
@v2 INTEGER
AS
DECLARE @id1 INTEGER,@id2 INTEGER,@newsameasid INTEGER
INSERT INTO noget (code) VALUES(@v1)
SELECT @id1 = SCOPE_IDENTITY()
INSERT INTO noget (code) VALUES(@v2)
SELECT @id2 = SCOPE_IDENTITY()
SELECT @newsameasid = ISNULL(MAX(sameasid),0)+1 FROM noget
UPDATE noget SET sameasid=@newsameasid WHERE id=@id1 OR id=@id2
GO
EXEC usp_pairinsert 1,2
GO
EXEC usp_pairinsert 3,4
GO
EXEC usp_pairinsert 5,7
GO
SELECT * FROM noget
GO
EXEC usp_pairinsert 6,8
GO
SELECT * FROM noget
GO
DROP PROCEDURE usp_pairinsert
GO
DROP TABLE noget
GO
