Search code examples
sqlmonetdb

How can I get the current value of a sequence in MonedDB?


For example, in PostgreSQL I can write the following lines to increment the sequence and to get the current value:

SELECT nextval('sequence');
SELECT currval('sequence');

In MonetDB I know how to write the first line:

SELECT NEXT VALUE FOR "sequence";

How can I write the second line?


Solution

  • You can use get_value_for. Here is an example:

    CREATE SEQUENCE "seq1" AS INTEGER START WITH 42;
    SELECT next_value_for('sys','seq1'); -- returns 42 and increments seq1
    SELECT get_value_for('sys','seq1');  -- returns 43
    SELECT get_value_for('sys','seq1');  -- returns 43 again
    

    Note the different quotes around seq1, this is required. Also, you need the pass your schema name to the functions. In this case it is sys.