0

阅读此链接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

我认为问题在于如何返回获取结果表单游标。

4

1 回答 1

1

您正在尝试做的事情没有实施。游标旨在这样返回,以便客户端可以随心所欲地获取行。特别是对于大的结果。您将为此定义一个函数RETURNS refcursor

可以使其与变量的FOR LOOP显式分配一起使用OUT,但这与...结合起来很棘手RETURNS TABLE。您还必须OPEN使用 cursor,因为在 plpgsql 的上下文中与与SQL for cursorsDECLARE相同的关键字具有不同的含义。你必须DECLAREFETCH .. 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 函数中不能通过名称引用参数。

如果你真的想返回一个 table 的所有列,你可以进一步简化:

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;
于 2013-02-01T23:13:12.597 回答