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);