NEXTVAL(Lakehouse v1)
シーケンスから次の値を取得します。
SQLの構文
Section titled “SQLの構文”NEXTVAL(<sequence_name>)整数。
この例では、シーケンスを使用したNEXTVAL関数の動作を示します。
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 │└─────────────────────────────────────────────────────┘この例では、シーケンスとNEXTVAL関数を使用して、テーブルの行に一意の識別子を自動的に生成し割り当てる方法を示します。
-- Create a new sequence named staff_id_seqCREATE SEQUENCE staff_id_seq;
-- Create a new table named staff with columns for staff_id, name, and departmentCREATE 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 columnINSERT 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 columnINSERT 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 │└───────────────────────────────────────────────────────┘