0

I got this

DECLARE
inserted_user authentication.users;
inserted_favorite user_settings.favorite_beneficiary;    
BEGIN
        IF $6 = 'xxxxx-xxx-xx-xxxx-xxxxxxxxxxxx' THEN
            WITH inserted_user AS (
                INSERT INTO authentication.users (id, email, first_name, last_name, phone_number, role_id, additional_information)
                VALUES ($1, $2, $3, $4, $5, $6, $7)
                ),inserted_favorite AS(
                INSERT INTO user_settings.favorite_beneficiary (user_id, beneficiary_id)
                VALUES ($1, $8)
                )
        END IF;
        RAISE EXCEPTION 'unauthorized';
    END;

But I got this message error, ERROR: syntax error at or near "END" LINE 16: END IF;

Did I miss something ? Can I write it on this way ?

1
  • 1
    You're missing a query after your WITH, a CTE must be followed by a SELECT/INSERT/... Commented Oct 28, 2020 at 9:05

1 Answer 1

3

Your CTE ("with clause") doesn't have a final query. You need to use:

WITH inserted_user AS (
    INSERT INTO authentication.users (id, email, first_name, last_name, phone_number, role_id, additional_information)
    VALUES ($1, $2, $3, $4, $5, $6, $7)
) --<< no second CTE here
INSERT INTO user_settings.favorite_beneficiary (user_id, beneficiary_id)
VALUES ($1, $8)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.