This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Sequence Functions

This section provides reference information for sequence functions in PlaidCloud Lakehouse.

1 - NEXTVAL

Retrieves the next value from a sequence.

SQL Syntax

NEXTVAL(<sequence_name>)

Return Type

Integer.

SQL Examples

This example demonstrates how the NEXTVAL function works with a sequence:

CREATE SEQUENCE my_seq;

SELECT
  NEXTVAL(my_seq),
  NEXTVAL(my_seq),
  NEXTVAL(my_seq);

┌─────────────────────────────────────────────────────┐
 nextval(my_seq)  nextval(my_seq)  nextval(my_seq) 
├─────────────────┼─────────────────┼─────────────────┤
               1                2                3 
└─────────────────────────────────────────────────────┘

This example showcases how sequences and the NEXTVAL function are employed to automatically generate and assign unique identifiers to rows in a table.

-- Create a new sequence named staff_id_seq
CREATE SEQUENCE staff_id_seq;

-- Create a new table named staff with columns for staff_id, name, and department
CREATE TABLE staff (
    staff_id INT,
    name VARCHAR(50),
    department VARCHAR(50)
);

-- Insert a new row into the staff table, using the next value from the staff_id_seq sequence for the staff_id column
INSERT INTO staff (staff_id, name, department)
VALUES (NEXTVAL(staff_id_seq), 'John Doe', 'HR');

-- Insert another row into the staff table, using the next value from the staff_id_seq sequence for the staff_id column
INSERT INTO staff (staff_id, name, department)
VALUES (NEXTVAL(staff_id_seq), 'Jane Smith', 'Finance');

SELECT * FROM staff;

┌───────────────────────────────────────────────────────┐
     staff_id           name           department    
├─────────────────┼──────────────────┼──────────────────┤
               2  Jane Smith        Finance          
               1  John Doe          HR               
└───────────────────────────────────────────────────────┘