Обсуждение: usring CURSORS within functions

Поиск
Список
Период
Сортировка

usring CURSORS within functions

От
"SunMie Won"
Дата:
Any tips on how to use a cursor within a function?
Does the DECLARE come after the BEGIN ( as all the
documentation suggests) or in the typical place for
declarations (after AS and before BEGIN). I have tried
both places and when running the function, get the
following error:

ERROR: parse error at or near CURSOR

Below is the syntax for my function:
CREATE FUNCTION "fnc_testcursor"("int4") RETURNS
"int4" AS
'DECLARE c_seating CURSOR FOR SELECT id_seat FROM
tbl_seating WHERE id_order = 0;
BEGIN FETCH 1 FROM c_seating;
CLOSE c_seating;
END;'
LANGUAGE 'plpgsql';

OR version 2:

CREATE FUNCTION "fnc_testcursor"("int4") RETURNS
"int4"
AS
 '
BEGIN
DECLARE c_seating CURSOR FOR SELECT id_seat FROM
tbl_seating WHERE id_order = 0;
FETCH 1 FROM c_seating;
CLOSE c_seating;
END;'
LANGUAGE 'plpgsql';

Both functions return the same error when called.
Thanks in advance-
SunMie Won


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


Re: usring CURSORS within functions

От
Tom Lane
Дата:
"SunMie Won" <sunmiewon@yahoo.com> writes:
> Any tips on how to use a cursor within a function?

You don't, at least not before 7.2.  The 7.2 support for cursors
in plpgsql is documented in the development docs:
http://candle.pha.pa.us/main/writings/pgsql/sgml/plpgsql-cursors.html
I don't really know why the syntax is what it is; it's a tad odd and
not anything like the normal SQL DECLARE x CURSOR.  Possibly Jan
borrowed it from Oracle.

            regards, tom lane