Home > Back-end >  How to `ITERATE` in a `FOR` loop?
How to `ITERATE` in a `FOR` loop?

Time:11-04

person_loop: FOR person IN (
    SELECT id FROM person
) DO
    IF person.id MOD 2 = 0 THEN
        ITERATE person_loop;
    END IF;

    SELECT person.id;
END FOR person_loop;

This code doesn't work in MariaDB 10.4.24; I get the following error due to the use of ITERATE:

DB ERROR 1241 (21000): Operand should contain 1 column(s)

Is there any way to do the equivalent of calling ITERATE in a loop (e.g. LOOP, WHILE) in a FOR loop, or do I have to just resort to doing things the old, clunky way using cursors and FETCH?

CodePudding user response:

I ended up employing this strategy:

FOR person IN (
    SELECT id FROM person
) DO
    loop_body: BEGIN
        IF person.id MOD 2 = 0 THEN
            LEAVE loop_body;
        END IF;

        SELECT person.id;
    END loop_body;
END FOR;
  • Related