<<<<<<< HEAD currval | H2 | SQLDoc
Skip to main content

currval

1. CURRVAL Function

======= currval | H2 Function | SQLDoc

currval

1. CURRVAL Function

>>>>>>> 9d19b1c8cb744c8e86d5c15b74d8b5f719ad62be

The CURRVAL function in H2 database is used to retrieve the current value of a sequence. It is typically used after using the NEXTVAL function to generate a new value from a sequence.

2. Syntax

The syntax for the CURRVAL function in H2 database is as follows:

CURRVAL(sequence_name)

Arguments

  • sequence_name: The name of the sequence from which you want to retrieve the current value.

Return

  • The CURRVAL function returns the current value of the specified sequence.

3. Notes

  • The CURRVAL function in H2 database can only be used after using the NEXTVAL function on the same sequence within the same session.
  • If the CURRVAL function is called without using the NEXTVAL function first or if the sequence has not been initialized, it will throw an exception.
  • The CURRVAL function is typically used in SELECT statements to retrieve the current value of a sequence and use it in further operations.

4. Examples

Here are a few examples demonstrating the usage of the CURRVAL function in H2 database:

Example 1 - Retrieving the current value of a sequence:

CREATE SEQUENCE my_sequence;

-- Generate a new value from the sequence
SELECT NEXTVAL('my_sequence');

-- Retrieve the current value from the sequence
SELECT CURRVAL('my_sequence') AS current_value;

Output:

current_value
-------------
1

Example 2 - Using CURRVAL in a SELECT statement:

-- Generate a new value from the sequence
SELECT NEXTVAL('my_sequence');

-- Retrieve the current value and use it in a SELECT statement
SELECT id, name
FROM my_table
WHERE id = CURRVAL('my_sequence');

Output:

id | name
---+------
1 | John
  • nextval - Generate the next value from a sequence.