简体   繁体   中英

HSQLDB: raise the counter of a SEQUENCE by N

In HSQLDB, I try to raise the counter of a sequence by N. I could not find any example of how to do it when N > 1, so I tried calling NEXT VALUE FOR my_sequence N times, however that also did not work out.

For example:

DROP PROCEDURE IF EXISTS my_procedure;
CREATE PROCEDURE my_procedure(OUT my_output INTEGER, OUT out_a_1 INTEGER, OUT out_a_2 INTEGER, OUT out_a_3 INTEGER, IN my_input INTEGER)
        MODIFIES SQL DATA -- NO SQL
        BEGIN ATOMIC

                DECLARE a_1 BIGINT;
                DECLARE a_2 BIGINT;
                DECLARE a_3 BIGINT;

                SET my_output = my_input;

                SET a_1 = NEXT VALUE FOR my_sequence;
                SET a_2 = NEXT VALUE FOR my_sequence;
                SET a_3 = NEXT VALUE FOR my_sequence;

                SET out_a_1 = a_1;
                SET out_a_2 = a_2;
                SET out_a_3 = a_3;
        END;

Result: the counter is raised only by 1 so that out_a_1 == out_a_2 == out_a_3

Is it possible to raise the counter by N in a single operation?

If not, what is the way to do it?

You need to insert the sequence value into a table. Each row insert will increment the sequence.

DECLARE TABLE T (ID INT)
INSERT INTO T VALUES (NEXT VALUE FOR my_sequence), (NEXT VALUE FOR my_sequence)

The example above can be used to increment the sequence by two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM