Nextval
and currval
are not functions, but are a " sequence of pseudo-commands .
"In one SQL statement containing a reference for NEXTVAL, Oracle increments the sequence once: for each row returned by the external query block of the SELECT statement. The query block may appear in the following places ..." (emphasis added) [ Oracle Database Reference , "How to use sequence values" ]
In other words, seq.nextval
not a side-effect function, but a pseudo- seq.nextval
that has a specific value for each row. When there is one link to seq.nextval
, the value is incremented for each row, regardless of whether that value is used. The result of OP sees peculiar to sequences, but not to case expression. For example, the same with decoding:
SQL> select decode(deptno 2 , 10, seq.nextval || 'next10' 3 , 20, seq.nextval || 'next20' 4 , 30, seq.currval || 'curr30') 5 from emp; DECODE(DEPTNO,10,SEQ.NEXTVAL||'NEXT10',20,SEQ. ---------------------------------------------- 35next20 36curr30 37curr30 38next20 39curr30 40curr30 41next10 42next20 43next10 44curr30 45next20 46curr30 47next20 48next10
source share