Db2 Sequence NEXT VALUE Guide | Generated by AI

Home 2025.09

In IBM Db2, NEXT VALUE FOR (sometimes written as NEXTVAL) is used to retrieve the next number from a sequence object. Sequences are database objects that generate numeric values, typically used for primary keys, surrogate keys, or any case where you need unique, incrementing values.


How It Works


Example

-- Create a sequence starting from 100, incrementing by 1
CREATE SEQUENCE my_seq
  START WITH 100
  INCREMENT BY 1
  NO CYCLE;

-- Use it in a query
SELECT NEXT VALUE FOR my_seq FROM sysibm.sysdummy1;

-- Insert into a table
INSERT INTO employees (id, name)
VALUES (NEXT VALUE FOR my_seq, 'Alice');

If you run the SELECT multiple times, you’ll get 100, 101, 102, and so on.


Key Notes


✅ In short: NEXT VALUE FOR in Db2 increments the sequence and gives you the next available number, ensuring uniqueness and consistency across sessions.

Would you like me to also show you how this differs from identity columns in Db2?


Back Donate