I wrote the snippet of PL/SQL today:
declare
first_id number;
second_id number;
begin
insert into table (sort_nr, text_id, unit_id) values(...,
table_seq.nextval, table_seq.nextval) returning text_id, unit_id
into first_id, second_id;
dbms_output.put_line(first_id);
dbms_output.put_line(second_id);
end;
and received a unique constaint violation. After futher examination, I discovered that first_id
and second_id
had the same value in it.
My question is: Is there any restriction calling the same sequence multiple times in the same statement and receiving a subsequent number? From my point of view it seems like the nextval
is invoked only once in the scope of the query and cached.
Just a side note, I cannot change that crappy schema to avoid using the same sequence in two columns.
Best Answer
This is the expected behaviour of
nextval
as documented:This means that you won't be able to use plain SQL to overcome this limitation: you will need some PL/SQL. Either a trigger that populates both fields or a function that wraps the sequence call. Here's an example with such a function with 11g: