1

I am trying to raise an issue in a Postgresql function using a concatenated message, but I get an error message at runtime:

error: syntax error at or near "msg"

The code is the following:

CREATE OR REPLACE FUNCTION insertUserAccount(
    id             bigint,
    nname          varchar(40),
    email          varchar(40),
    pwd            varchar,
    status         smallint,
    last_update  bigint,
    preferences    json,
    bits           integer,
    adm            json)
    RETURNS bigint AS $$
DECLARE
    rowc INTEGER;
    ret_id bigint;
    msg text;
BEGIN
    ...
    IF ( rowc > 0 ) THEN
        msg = 'User account already exists or name or email is unavailable (id=' || id
            || ', name=' || nname
            || ', email=' || email || ')';
        RAISE EXCEPTION msg USING ERRCODE = '23505';
    ELSE
    ...
3
  • Use := for assignment in PL/pgSQL. Commented Mar 20, 2014 at 8:39
  • Thanks, if you create an answer, I'll approve it. Commented Mar 20, 2014 at 8:49
  • 1
    While := is good advice, it cannot explain the error. Details here. Syntax options for RAISE depend on your version of Postgres. There have been several updates in recent versions. Commented Mar 20, 2014 at 9:42

2 Answers 2

3

The actual problem is the faulty syntax for RAISE EXCEPTION. I would simplify overall:

IF rowc > 0 THEN
   RAISE EXCEPTION 'User account already exists or name or email is unavailable (id=%,name=%,email=%)'
                 , id, nname, email  USING ERRCODE = '23505';
ELSE ...

The variable msg might not be needed at all then. It is generally best to keep the number of assignments low, since those are rather expensive in PL/pgSQL (as compared to other programming languages). Not dramatic, but still ..

Sign up to request clarification or add additional context in comments.

Comments

1

Use := for assignment in PL/pgSQL

1 Comment

I had to change RAISE EXCEPTION msg USING ERRCODE = '23505'; into RAISE EXCEPTION '%', msg USING ERRCODE = '23505'; to make it work too.

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.