Re: ERROR: syntax error at or near "IF"... why?

From: Johan Nel <johan555(dot)nel555(at)xsinet555(dot)co(dot)za>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: ERROR: syntax error at or near "IF"... why?
Date: 2009-04-30 05:30:45
Message-ID: gtbd3e$k9$1@news.eternal-september.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Daniel,

> 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?

SELECT returns in essence a record or setof records.

DECLARE _credit int;
...
SELECT credit FROM users WHERE name = 'mary' INTO _credit;
IF _credit < 0 THEN
ROLLBACK;
END IF;

If there is a chance that the select returns more than one record you
can do something similar to:
DECLARE rec record;
...
FOR rec IN (SELECT credit FROM users WHERE name = 'mary'
LOOP
IF rec.credit < 0 THEN
...
ELSE
...
END IF;
END LOOP;

HTH,

Johan Nel
Pretoria, South Africa.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2009-04-30 06:13:50 Re: recover corrupt DB?
Previous Message Peeyush Jain 2009-04-30 04:50:36 Issue with SSAS