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

Поиск
Список
Период
Сортировка
От Johan Nel
Тема Re: ERROR: syntax error at or near "IF"... why?
Дата
Msg-id gtbd3e$k9$1@news.eternal-september.org
обсуждение исходный текст
Ответ на ERROR: syntax error at or near "IF"... why?  (DaNieL <daniele.pignedoli@gmail.com>)
Список 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.

В списке pgsql-general по дате отправления:

Предыдущее
От: Raymond O'Donnell
Дата:
Сообщение: Re: Do TEMP Tables have an OID? Can this be a problem if used too frequently?
Следующее
От: "Daniel Verite"
Дата:
Сообщение: Re: Importing large objects from the client side programatically.