日期:2014-05-17  浏览次数:20784 次

谁能帮我优化下这段代码
gc_ok varchar2(10) := 'ok';
  gc_pagelen number(10) := 100 ; 

--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--

procedure paging(
  vi_ymd in date,
  vi_pagelen in out integer, -- 0: 不用分页,查询出全部记录。-1:缺省页长度
  vi_pagenum in out integer,  
  vo_msg out varchar2,
  vo_total out integer,
  vo_hasnext out integer, -- 0: have not next records; 1: hasnext is true
  vo_cursor out sys_refcursor
)is
 
  v_ymd date;
  v_pagehead number;
  v_pagetail number;
  v_pagelen number;
  v_count integer;
  
begin
  
  vo_msg := gc_ok;
  v_ymd := trunc(vi_ymd);
  
  if vi_pagelen > 0 then
  v_pagelen := vi_pagelen;
  elsif vi_pagelen < 0 then
  v_pagelen := gc_pagelen;
  end if;
  
  v_pagehead := vi_pagenum * v_pagelen + 1; --第一个记录rownum
  v_pagetail := vi_pagenum * v_pagelen + v_pagelen; --最后一个记录rownum
   
  select count(*) into v_count from 
  ( select rownum row_no, x.* from
  ( select b.orgidt, b.cusidt, b.apcode, b.curcde, b.actnam, a.bal, a.avgbal
  from t_bal a, t_act b
  where a.ymd = v_ymd
  and a.act_id = b.id
  ) x
  ) y;

  if v_count > 0 then
   
  vo_total := v_count;  
   
  case vi_pagelen 
   
  when 0 then
  open vo_cursor for 
  select row_no, y.* from
  ( select rownum row_no, x.* from
  ( select b.orgidt, b.cusidt, b.apcode, b.curcde, b.actnam, a.bal, a.avgbal
  from t_bal a, t_act b
  where a.ymd = v_ymd
  and a.act_id = b.id
  ) x
  ) y;
   
  when -1 then 
   
  open vo_cursor for 
  select row_no, y.* from
  ( select rownum row_no, x.* from
  ( select b.orgidt, b.cusidt, b.apcode, b.curcde, b.actnam, a.bal, a.avgbal
  from t_bal a, t_act b
  where a.ymd = v_ymd
  and a.act_id = b.id
  ) x
  ) y
  where row_no between v_pagehead and v_pagetail;
   
  else
   
  open vo_cursor for 
  select row_no, y.* from
  ( select rownum row_no, x.* from
  ( select b.orgidt, b.cusidt, b.apcode, b.curcde, b.actnam, a.bal, a.avgbal
  from t_bal a, t_act b
  where a.ymd = v_ymd
  and a.act_id = b.id
  ) x
  ) y
  where row_no between v_pagehead and v_pagetail;
   
  end case;
  else 
   
  open vo_cursor for 
  select null orgidt,null cusidt,null apcode,null curcde,
  null actnam,null bal,null avgbal from dual;
  return;
   
  end if;
  
  if vi_pagenum * vi_pagelen + gc_pagelen < v_count then
  v