PostgreSQL "IF" syntax error

34,245

Solution 1

As Johannes already says: you are mixing regular SQL with PL/pgSQL, the stored procedure language. The link that Johannes provides should explain the concept of stored procedures to you.

I take it you're doing this as a script? Executing one statement after another? I'm afraid you can only do what you want to do inside a Stored Procedure, or Function, as you might call it. This is because when you are executing statements in this way, every statement stands on its own with no relation or information regarding the other statements.

Furthermore you can look at the following link for more information on how to use IF ... THEN ... ELSE ... END IF; conditionals inside plpgsql: link.


EDIT:

I don't know if ROLLBACK is allowed at that point (because each stored procedure is already in its own transaction), but you must be able to figure that out for yourself using the extensive documentation @ http://www.postgresql.org. Here's a sample function with your code in it, also demonstrating some other syntax:

CREATE OR REPLACE FUNCTION public.test()
RETURNS integer AS
$$
DECLARE
tempvar integer;

BEGIN    
     tempvar := 1;

     INSERT INTO movements (from, to, import) VALUES ('mary', 'steve', 600);
     UPDATE users SET credit = credit - 600 WHERE name = 'mary';
     UPDATE users SET credit = credit + 600 WHERE name = 'steve';

     --here comes the problem!
     IF (SELECT credit FROM users WHERE name = 'mary') < 0 THEN
        ROLLBACK;
     END IF;

     RETURN tempvar;
END
$$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER;

However, if you are really going this road, I recommend using a GUI DB manager. It's easier for learning all this.

Solution 2

You seem to use plain SQL but the IF statement is part of the PL/pgSQL procedural language which is part of PostgreSQL.

Solution 3

You could try to modify the IF part, from:

IF (SELECT credit FROM users WHERE name = 'mary') < 0 THEN
 ROLLBACK;
END IF

to

SELECT SUM(credit) INTO v_credit FROM users WHERE name = 'mary';
IF (v_credit) < 0 THEN
 ROLLBACK;
END IF

Assuming v_credit is a variable you defined previously. IMHO, Postgre assumes SELECT query returns more than one result, even though you're very certain that it's unique. So I think you could try to assign the value to a variable first beforehand.

Solution 4

If you want to avoid the if you could rewrite your code as:

BEGIN;

    INSERT INTO movements (from, to, import)    
    SELECT 'mary', 'steve', CASE credit < 600 WHEN TRUE THEN 0 ELSE 600 END;

    UPDATE users SET credit = credit - CASE credit < 600 WHEN TRUE THEN 0 ELSE 600 END    
    WHERE name = 'mary';

    UPDATE users u SET u.credit = u.credit + CASE v.credit < 600 WHEN TRUE THEN 0 ELSE 600 END    
    FROM users v    
    WHERE u.name = 'steve' and v.name = 'mary'

COMMIT;

Yes, this is stupid :) .

Share:
34,245
Strae
Author by

Strae

I can accept failure, everyone fails at something - But I can't accept not trying. -- You HAVE to assume your visitor is a maniac serial killer, out to destroy your application. And you have to prevent it. Hire me

Updated on March 02, 2020

Comments

  • Strae
    Strae about 4 years

    I'm new with PostgreSQL, and I already have my first problem..

    I wrote some code to understand how transactions work, following the manual step by step.

    To make it short, I've created 2 tables, user and movements: in the first one there are the name, email and credit columns, in the second the columns from, to, import.

    So, I was trying this way:

    BEGIN;
    INSERT INTO movements (from, to, import) VALUES ('mary', 'steve', 600);
    UPDATE users SET credit = credit - 600 WHERE name = 'mary';
    UPDATE users SET credit = credit + 600 WHERE name = 'steve';
    --here comes the problem!
    IF (SELECT credit FROM users WHERE name = 'mary') < 0 THEN
     ROLLBACK;
    END IF
    COMMIT;
    

    I always get the error:

    ERROR: syntax error at or near "IF"

    Where am I mistaken?

    P.S.: Don't focus on the example functionality, it's just a trial for me to understand the transactions.. and now, the IF clause...

  • Strae
    Strae about 15 years
    Ok, i have understood and im reading the johannes link (that i didn't see first time i read the answer). Can you post an example of how may look the sql i wrote in PL/pgSQL? Thanks..
  • Strae
    Strae about 15 years
    lol im new of postgresql.. i think to have understood your example ;)
  • Adam Luter
    Adam Luter about 15 years
    It's a terrible example, really, it was just a thought experiment :) .
  • Strae
    Strae about 15 years
    for testing it, i've even tryed the IF 2 = 2 THEN [...] but still throw the error..
  • Strae
    Strae about 15 years
    Thanks mate, i've found the postgres documentation very good and well-written.. just little 'big', but that's ok, i've purchased the 3 volume paper version and i promise to read them all. I just cant understand why the if statement cant be used in plain sql when, in mysql, it can ben used as well..
  • pyrocumulus
    pyrocumulus about 15 years
    The operator ':=' is used for assigning variables their values without a corresponding query.