阅读此链接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
我认为问题在于如何返回获取结果表单游标。