I have a procedure which inserts record into a table. I also have a trigger which auto-increments sequence on BEFORE INSERT (also there is identity type). I'd like to operate with its result, e.g. highlight a new record in a table on a website for example. Is usage of RETURNS with GEN_ID(<sequence>, 0) the only way? If so, there is a race condition, as far as I know, isn't it? My ID may not be mine if someone's transaction succeeds first.
There is also a RETURNING clause, but as far as I know it could be used in DML queries like in execute block or dynamic statements as in INSERT INTO ... RETURNING ID?
To return a generated id from a stored procedure, all you have to do is use the
RETURNINGclause in yourINSERTstatement, and assign it to one of the output variables of the stored procedure.As a simple example:
You can then execute this as:
For example: