在plpgsql中获取游标

时间:2013-01-31 19:49:41

标签: sql postgresql cursor plpgsql

阅读此链接http://microjet.ath.cx/WebWiki/ResultPaginationWithPostgresql.html后,我决定使用光标进行分页。但似乎我不知道如何在plpgsql中获取结果。

这是我的功能

CREATE OR REPLACE FUNCTION get_pagination_custom_word_moderation(_moderation_id bigint, _is_black boolean, _index integer, _max_result integer)
    RETURNS TABLE(word_id bigint,
        word character varying,
        is_num_rlpcm boolean,
        is_word_bund boolean,
        note text,
        create_time timestamp without time zone,
        last_update timestamp without time zone) AS
$BODY$
DECLARE custom_word_moderation_cursor CURSOR FOR
    SELECT
        word_id,
        word,
        is_num_rlpcm,
        is_word_bund,
        note,
        create_time,
        last_update
    FROM
        custom_word_moderation
    WHERE
        moderation_id=_moderation_id
    AND is_black=_is_black;
BEGIN

MOVE ABSOLUTE _index FROM custom_word_moderation_cursor;
RETURN QUERY FETCH _max_result FROM custom_word_moderation_cursor;

END;
$BODY$
  LANGUAGE plpgsql VOLATILE;

错误是:

ERROR:  syntax error at or near "$1"
LINE 1:  FETCH  $1  FROM  $2 
                ^
QUERY:   FETCH  $1  FROM  $2 
CONTEXT:  SQL statement in PL/PgSQL function "get_pagination_custom_word_moderation" near line 18

********** Error **********

ERROR: syntax error at or near "$1"
SQL state: 42601
Context: SQL statement in PL/PgSQL function "get_pagination_custom_word_moderation" near line 18

我认为问题在于如何返回获取结果形式的游标。

1 个答案:

答案 0 :(得分:1)

您要执行的操作未实现。游标旨在以这样的方式返回,以便客户端可以随意获取行。特别是对于大结果。您可以使用RETURNS refcursor定义一个函数。

可以使用FOR LOOP并明确分配OUT变量,但与RETURNS TABLE结合起来很棘手...... 您还必须OPEN the cursor,因为DECLARE在plpgsql的上下文中具有与SQL DECLARE for cursors相同的关键字的不同含义。你必须FETCH .. INTO ..

相反,使用没有光标的简单等效项:

CREATE OR REPLACE FUNCTION get_pagination_custom_word_moderation(
                         _moderation_id bigint, _is_black boolean
                       , _index integer, _max_result integer)
    RETURNS TABLE(word_id bigint,
        word varchar,
        is_num_rlpcm boolean,
        is_word_bund boolean,
        note text,
        create_time timestamp,
        last_update timestamp) AS
$func$
BEGIN
   RETURN QUERY
   SELECT word_id
         ,word
         ,is_num_rlpcm
         ,is_word_bund
         ,note
         ,create_time
         ,last_update
   FROM   custom_word_moderation
   WHERE  moderation_id = _moderation_id
   AND    is_black = _is_black
   OFFSET _index
   LIMIT  _max_result;
END
$func$ LANGUAGE plpgsql;

甚至更简单的SQL函数:

CREATE OR REPLACE FUNCTION get_pagination_custom_word_moderation(
                         _moderation_id bigint, _is_black boolean
                       , _index integer, _max_result integer)
    RETURNS TABLE(word_id bigint,
        word varchar,
        is_num_rlpcm boolean,
        is_word_bund boolean,
        note text,
        create_time timestamp,
        last_update timestamp) AS
$func$
   SELECT word_id
         ,word
         ,is_num_rlpcm
         ,is_word_bund
         ,note
         ,create_time
         ,last_update
   FROM   custom_word_moderation
   WHERE  moderation_id = $1
   AND    is_black = $2
   OFFSET $3
   LIMIT  $4;
$func$ LANGUAGE sql;

我在函数体中使用$n表示法,因为在9.2版之前的SQL函数中,参数不能通过名称引用。

如果您确实想要返回表格的所有列,您可以进一步简化:

CREATE OR REPLACE FUNCTION get_pagination_custom_word_moderation(
                         _moderation_id bigint, _is_black boolean
                       , _index integer, _max_result integer)
    RETURNS SETOF custom_word_moderation AS
$func$
   SELECT *
   FROM   custom_word_moderation
   WHERE  moderation_id = $1
   AND    is_black = $2
   OFFSET $3
   LIMIT  $4;
$func$ LANGUAGE sql;