Everyone knows Oracle doesn’t have auto generated columns, so what am I talking about? I really mean columns whose value is generated by a trigger. The most common example being to simulate the auto-generated id columns functionality found in many other databases. In Oracle instead of defining a column as having an auto generated numeric value you have to define a sequence, define an insertion trigger that selects the next sequence value and uses it for the inserted id columns value, something like the following:
|
|
This works fine, you can insert into the table in the manner shown below, and the trigger will generate a value for the id column:
|
|
Very often however, you want to know the value of the id column that was chosen by the trigger. You could do something like this:
|
|
The above will work IFF there is a unique constraint on the name column, it won’t work reliably if not, there could be more than one row with the value ‘whatever’. Anyways having to perform two statements (the insert and then the select) seems very heavyweight for what is a very common need, there must be a better way. The better way to do this is to use the returning into clause with the insert statement, like so:
|
|
That’s a bit less verbose and more robust at the same time. It’s worth emphasising that this technique is not just limited to auto-generated ids, it can be applied to any column value that may be generated or modified by a trigger.