• Postgres通用翻页函数


    CREATE OR REPLACE FUNCTION fun_turnpage(
    PageSize INT,
    PageIndex INT,
    FldSort VARCHAR,
    StrCondition VARCHAR
    )
    RETURNS SETOF record AS
    $BODY$
    DECLARE
    select_result record;
    PageCount INT;
    Counts INT;
    BEGIN
    --总记录条数
    EXECUTE 'SELECT COUNT(*) from
    (
    ' || StrCondition || '
    ) A' INTO Counts;

    --总页码
    PageCount:=CEIL(CAST(Counts AS NUMERIC)/CAST(PageSize AS NUMERIC));

    --翻页的记录明细
    FOR select_result IN
    EXECUTE 'SELECT * FROM
    (
    SELECT *,ROW_NUMBER() OVER
    (
    ORDER BY ' || FldSort || '
    ) rn,
    ' || PageCount || ' AS pagecount,
    ' || Counts || ' AS counts
    FROM (' || StrCondition || ') _Data
    ) Result
    WHERE Result.RN >' || PageSize *(PageIndex-1) || ' AND Result.RN <=' || PageSize*PageIndex
    LOOP
    RETURN NEXT select_result;
    END LOOP;
    RETURN;
    END;
    $BODY$ LANGUAGE plpgsql;
    SELECT * FROM fun_turnpage(50,1,'username asc','select * from tb_user')
    AS
    Users(userid character VARYING(36), username VARCHAR(36),rn BIGINT,pagecount INT,counts INT);

  • 相关阅读:
    async源码学习
    js 数组去重
    node通过http.request向其他服务器上传文件
    学习CSS布局
    学习CSS布局
    学习CSS布局
    学习CSS布局
    学习CSS布局
    学习CSS布局
    学习CSS布局
  • 原文地址:https://www.cnblogs.com/littlewrong/p/9870916.html
Copyright © 2020-2023  润新知