Oracle按限制筛选结果

时间:2019-05-03 09:50:58

标签: oracle

我是Oracle的新手,希望有人能帮助我。 我有这个存储过程:

procedure ListCatalogues(P_CUR out sys_refcursor,
                         P_CATALOGUENAME varchar2 default '%',
                         P_LIMIT number,
                         P_MEMBERS number default -1) is

begin
     open P_CUR for
        select * 
          from (  select h.catalogueid                           id,
                         h.cataloguename                         name,
                         case 
                         when h.uniquecatalogue = 'N' 
                         then 1 
                         else 0 
                         end                                     includeproducts,
                         case
                         when h.active = 'Y' 
                         then 1 
                         else 0 
                         end                                     active,
                         case
                         when h.ownbrandedlabels = 'Y' 
                         then 1 
                         else 0 
                         end                                     ownlabels,
                         (  select count(*) 
                              from cc_custprofiles t 
                             where t.catalogueid = h.catalogueid
                         )                                       members                 

                    from cc_ob_catalogueheader h

                   where upper(h.cataloguename) like upper('%'||P_CATALOGUENAME||'%')
                     and (select count(*) from cc_custprofiles t where t.catalogueid = h.catalogueid) >= P_MEMBERS

                order by h.catalogueid
               ) 

         where rownum <= P_LIMIT;

end ListCatalogues;

如您所见,它接受一个P_LIMIT参数,该参数允许限制返回的结果。很好,但是我想在此基础上再扩展一点。

如果限制为10,则返回10行,但是如果限制为0,则返回所有行。有人可以帮我更改查询以符合我的条件吗?

1 个答案:

答案 0 :(得分:0)

经过一番环顾后,我设法做到了:

where rownum <= case when P_LIMIT = 0 then rownum else P_LIMIT end;