There's a better way. Try something like:
DECLARE
nPK_col NUMBER;
nCol1 NUMBER := 1;
nCol2 NUMBER := 2;
BEGIN
INSERT INTO SOME_TABLE(COL1, COL2)
VALUES (nCol1, nCol2)
RETURNING PK_COL INTO nPK_col;
END;
This example assumes that the primary key column named PK_COL
is populated in some way during the execution of the INSERT
statement, e.g. by a trigger. The RETURNING
clause of the INSERT
statement specifies that the value of PK_COL
from the inserted row should be put into the variable specified, in this case nPK_col
. You can specify multiple columns and variables in the RETURNING
clause - documentation here. You may need to put this into whatever procedure performs the actual INSERT
and then add an OUT
parameter to allow the value to be passed back to the caller - or use a FUNCTION
instead of a PROCEDURE
and have the primary key value be the return value of the FUNCTION
.
Share and enjoy.