Postgre SQL附近的语法不正确

时间:2013-07-11 06:18:33

标签: postgresql postgresql-9.1 postgresql-9.2 rails-postgresql

我正在尝试创建函数,但它在if语句中有错误,不知道它有什么问题。

CREATE FUNCTION uspgetcountrylistwithpagenumber(IN "PageNumber" integer, IN "PageSize"         integer, IN "WhereClause" text, IN "OrderBy" text) RETURNS SETOF getcountrylistitem AS
$BODY$declare 
RowData "getcountrylistitem"%rowtype;

    IF $1 <> -1 and $2 <> -1  then
        for RowData in
        loop
            execute 'Select "CountryId","CountryName"' ||
                    'from "MasterCountry"' ||
                    $3 || 
                    $4 ||
                    'limit' || $2 ||
                    'offset' || ($1-1)*$2           
            RETURN NEXT RowData;
        END LOOP;
        RETURN;     
    else
        for RowData in
        loop
            execute 'Select "CountryId","CountryName"' ||
                    'from "MasterCountry"' ||
                    'order by "CountryName" asc'
            RETURN NEXT RowData;
            END LOOP;
            RETURN;
    end IF;
end;
$BODY$
LANGUAGE plpgsql IMMUTABLE;
ALTER FUNCTION public.uspgetcountrylistwithpagenumber(IN integer, IN integer, IN text, IN text)
OWNER TO postgres;

我想要实现的是自定义分页.. 我想要一个可以使用PageNumber,PageSize,WhereClause和OrderByClause的函数

PageNumber=1
PageSize=10
WhereClause=Where 1=1 and CountryName='INDIA'
OrderByClause=order by CountryName desc

以上是有助于解决我的问题的参数值 如果需要更多信息,请与我们联系。提前谢谢..:P

1 个答案:

答案 0 :(得分:1)

在第一个BEGIN之前需要IF