在PostgreSQL中返回数据类型函数

时间:2018-05-21 10:48:13

标签: postgresql

PostgreSQL注意到我的错误:

  

错误:未在查询中指定结果数据返回目标

这是我原创的功能代码:

    CREATE OR REPLACE FUNCTION public.scrolltable(
    IN p_pagenumber integer,
    IN p_pagesize integer)
  RETURNS TABLE(name character varying, id character varying, rn bigint) AS
$BODY$
    Declare v_startRow int;
 v_endRow int;Begin


    v_startRow := (p_pagenumber - 1) * p_pagesize + 1;
    v_endRow := p_pagenumber * p_pagesize;

    With Result as (
        Select *,
        ROW_NUMBER() OVER (Order By "ID" ASC)
        From "Training"
    )

    select * from Result
    Where row_number Between v_startRow AND v_endRow;

End;
$BODY$
  LANGUAGE plpgsql

我试图只运行选择查询,它显示如下表:

------------------------------------------------------------------------
|<Name>                   |  <ID>                      |  <row_number> |
|<character varying(100)> |  <character varying(100)>  |  <bigint>     |
------------------------------------------------------------------------

我已经为结果表返回了正确的数据类型,但为什么我仍然会收到此错误?

更新: 谢谢你的所有答案。我已经成功修复了这种错误:

CREATE OR REPLACE FUNCTION public.scrolltable(
    IN p_pagenumber integer,
    IN p_pagesize integer)
  RETURNS TABLE(name character varying, id character varying, rownumber bigint) AS
$BODY$
    Declare v_startRow int;
 v_endRow int;Begin


    v_startRow := (p_pagenumber - 1) * p_pagesize + 1;
    v_endRow := p_pagenumber * p_pagesize;

    RETURN QUERY With Result as (
        Select *,
        ROW_NUMBER() OVER (Order By "ID" ASC)
        From "Training"
    )

    select * from Result
    Where row_number Between v_startRow AND v_endRow;

End;
$BODY$
  LANGUAGE plpgsql

1 个答案:

答案 0 :(得分:0)

因为你写的是Postgres Function(pg / plsql),你需要在你的函数返回一些结果的最后一个语句中添加return query,像这样:

return query select * from Result
Where row_number Between v_startRow AND v_endRow;

这应该可以正常工作。