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

Поиск
Список
Период
Сортировка
От DaNieL..!
Тема Re: ERROR: syntax error at or near "IF"... why?
Дата
Msg-id b3bd908e-4245-44aa-8ee4-704c73147768@y6g2000prf.googlegroups.com
обсуждение исходный текст
Ответы Re: ERROR: syntax error at or near "IF"... why?  (Raymond O'Donnell <rod@iol.ie>)
Re: ERROR: syntax error at or near "IF"... why?  (Johan Nel <johan555.nel555@xsinet555.co.za>)
Re: ERROR: syntax error at or near "IF"... why?  (Jaime Casanova <jcasanov@systemguards.com.ec>)
Список pgsql-general
On 30 Apr, 07:30, Johan Nel <johan555.nel...@xsinet555.co.za> wrote:
> 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.

I tryed the declare, before and after the BEGIN;, but allways returns
me the error:
-----------------------------------------------------------
ERROR: syntax error at or near "int";
LINE 1: DECLARE _mycredit int;
                                           ^
-----------------------------------------------------------

For the if statement, i've tryed that
IF 2 = 2 THEN
 ROLLBACK;
END IF

but still the error near the "IF"..
dunno.. in mysql the if can be used in that way.. in postgres no?

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

Предыдущее
От: Alan Hodgson
Дата:
Сообщение: Re: pg_dump and pg_restore problem
Следующее
От: Raymond O'Donnell
Дата:
Сообщение: Re: ERROR: syntax error at or near "IF"... why?