I need to get the next value of a sequence twice in DB2 (version 9.1). Rather than executing the following twice:
SELECT nextval FOR schema.sequence AS id
FROM dual
I would like to do something like:
SELECT nextval FOR schema.sequence AS id1,
nextval FOR schema.sequence AS id2
FROM dual
Except the above only increments it once:
ID1 ID2
----------- -----------
643 643
1 record(s) selected.
Am I forced to just query twice, or is there a way to increment it twice in 1 query?
This is a a little complex, but it works (I verified by running it in DB2 v.9).
This is some of my favorite code, and it is recursive SQL. You can do it without the sequence object, and it will just generate several rows of numbers. I use this sometimes to insert rows of generated test data.