I’m trying to do this query (in oracle) but I have some problems:
SELECT CASE
WHEN deptno = '10' THEN scott.seq.nextval
|| 'next10'
WHEN deptno = '20' THEN scott.seqnextval
|| 'next20'
WHEN deptno = '30' THEN scott.seq.currval
|| 'curr'
END col_1
FROM scott.emp;
I’m getting this results:
COL_1 ---------------------------------------------- 191next20 192curr 193curr 194next20 195curr 196curr 197next10 198next20 199next10 200curr 201next20 202curr 203next20 204next10 205next20 206next10 207next10
And this is what I think they should be:
COL_1 ---------------------------------------------- 191next20 192curr 193curr 194next20 194curr 194curr 197next10 198next20 199next10 199curr 201next20 201curr 203next20 204next10 205next20 206next10 207next10
So, why i get the next value of the sequence also when I should have the current value and not only when the case selects the next value?
Yeah, this could be done with a plsql script but I can’t.
Thanks you!
Interesting. Per the Oracle docs:
Notice it doesn’t say the statements in the “true” WHEN clause. So even if the when statement is false, the nextval will fire:
I must admit this is different than I expected.
EDIT:
See answer from ShannonSeverance