Mathieu Arthur
Mathieu Arthur

Reputation: 31

How to fix END$$ error with a DO function in PostgreSql

I don't understand why I get an error on the line with the : "END $$"

DO $$
BEGIN
DECLARE 
    i INT := 0;
    j INT := i + 1;
    numberUsers INT;
    BEGIN
        SELECT COUNT(*) INTO numberUsers FROM "WU_Users";
        WHILE i <= numberUsers LOOP
        i := i + 1; 
    END LOOP;
END $$;

The error :

ERREUR:  erreur de syntaxe à la fin de l'entrée
LIGNE 12 : END $$;

Kind Regards !

Upvotes: 0

Views: 475

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246588

Indent properly, then it will become clear. The block structure is

DECLARE  /* optional */
BEGIN
EXCEPTION  /* optional */
END;

To format your code:

DO $$
BEGIN
    DECLARE 
        i INT := 0;
        j INT := i + 1;
        numberUsers INT;
    BEGIN
        SELECT COUNT(*) INTO numberUsers FROM "WU_Users";
        WHILE i <= numberUsers LOOP
            i := i + 1; 
        END LOOP;
    /* --> missing END; here <--- */
END $$;

Alternatively, you could remove the BEGIN at the beginning, since that outermost block just contains another block.

Upvotes: 1

Related Questions