Sequence functions in GoogleSQL

GoogleSQL for Spanner supports the following sequence functions.

Function list

NameSummary
GET_INTERNAL_SEQUENCE_STATEGets the current sequence internal counter before bit reversal.
GET_NEXT_SEQUENCE_VALUETakes in a sequence identifier and returns the next value. This function is only allowed in read-write transactions.

GET_INTERNAL_SEQUENCE_STATE

GET_INTERNAL_SEQUENCE_STATE(SEQUENCEsequence_identifier)

Description

Gets the current sequence internal counter before bit reversal. This function isuseful for import or export, and migrations. IfGET_NEXT_SEQUENCE_VALUE isnever called on the sequence, then this function returnsNULL.

Arguments

  • sequence_identifier: The ID for the sequence.

Return Data Type

INT64

Example

SELECTGET_NEXT_SEQUENCE_VALUE(SEQUENCEMySequence)ASnext_value;/*---------------------+ | next_value          | +---------------------+ | 5980780305148018688 | +---------------------*/
SELECTGET_INTERNAL_SEQUENCE_STATE(SEQUENCEMySequence)ASsequence_state;/*----------------+ | sequence_state | +----------------+ | 399            | +----------------*/

GET_NEXT_SEQUENCE_VALUE

GET_NEXT_SEQUENCE_VALUE(SEQUENCEsequence_identifier)

Description

Gets the next integer in a sequence.

Arguments

  • sequence_identifier: The ID for the sequence.

Return Data Type

INT64

Example

Create a table where its key column uses the sequence as a default value.

CREATETABLESingers(SingerIdINT64DEFAULT(GET_NEXT_SEQUENCE_VALUE(SEQUENCEMySequence)),aSTRING(MAX),)PRIMARYKEY(SingerId);

Obtain a sequence value in a read-write transaction and use it in an INSERTstatement.

SELECTGET_NEXT_SEQUENCE_VALUE(SEQUENCEMySequence)asnext_id;INSERTINTOSingers(SingerId,a)VALUES(next_id,1);

Use the sequence functions independently in the GoogleSQL DML.

INSERTINTOSingers(SingerId)VALUES(GET_NEXT_SEQUENCE_VALUE(SEQUENCEMySequence);

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.