I'm stuck on this pretty simple script. It isn't working like I expect it to.
declare
st VARCHAR(1024);
begin
for x in (SELECT sequence_name FROM USER_SEQUENCES) loop
st := 'ALTER SEQUENCE ' || x.sequence_name || ' INCREMENT BY 1000';
execute immediate st;
st := 'select ' || x.sequence_name || '.nextval from dual';
execute immediate st;
st := 'ALTER SEQUENCE ' || x.sequence_name || ' INCREMENT BY 1';
execute immediate st;
end loop;
end;
/
When I run this it doesn't appear to work at all - all of my sequences just stay as they are, and they have not been incremented by a thousand by the dynamic statements. If I check nextval
before and after the anonymous block, the difference is only 1, not 1001.
If I replace execute immediate
with dbms_output.put_line
and execute the generated commands manually the sequences are altered as I want.
What am I missing?
Both
alter sequence
statements are working, it's the increment in between that isn't happening. Thenextval
call in your loop is not being evaluated because the select statement isn't sending its output anywhere. From the documentation, a note that happens to refer to exactly what you are doing:So you need to select that value into something:
I've added a
val
variable, and aninto val
clause on the second execute immediate.To demonstrate that it works now:
Without the
into
clause, this came back with 1 rather than 1001, which is what you are seeing.The
restart start with
syntax in 12c can simplify the steps: