"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Resolve \"Unterminated Dollar-Quoted String\" Errors in PostgreSQL Functions with Goose?

How to Resolve \"Unterminated Dollar-Quoted String\" Errors in PostgreSQL Functions with Goose?

Published on 2024-11-17
Browse:196

How to Resolve \

Unterminated Dollar-Quoted String: Resolving Errors with Semicolons

In the context of creating a PostgreSQL function with Goose, this article addresses an error encountered while processing a complex statement within the function body. The error, reported by the pq library, indicates that a dollar-quoted string remains unterminated.

To resolve this issue, note that complex statements featuring semicolons require annotation using "-- goose StatementBegin" and "-- goose StatementEnd" annotations, as per the Goose documentation. These annotations assist Goose in managing embedded semicolons within SQL statements, preventing libpq errors.

Applying these annotations to the provided code sample resolves the error:

CREATE OR REPLACE FUNCTION add_userlocation(user_id INT, location_id INT) RETURNS VOID AS
$BODY$
--  goose StatementBegin
BEGIN
    LOOP
        UPDATE userslocations SET count = count 1 WHERE userid = user_id AND locationid = location_id;
    IF found THEN
        RETURN;
    END IF;
    BEGIN
        INSERT INTO userslocations(userid,locationid, count) VALUES (user_id, location_id, 1);
           RETURN;
       EXCEPTION WHEN unique_violation THEN
    END;
   END LOOP;
--  goose StatementEnd
END;
$BODY$
LANGUAGE plpgsql;
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3