Oacle 저장 프로시저 페이지

5949 단어 OaclesqlHibernate
본 논문 의 코드 는 나의 동료 진 위 의 작품 이다.
 
디자인 원 리 는 hibenate 의 페이지 방법 을 추출 하여 저장 과정 으로 쓰 고 조회 결 과 를 커서 로 되 돌려 주 는 것 이다.
 
첫 번 째 단계:
CREATE OR REPLACE PACKAGE pack AS
  TYPE cur IS REF CURSOR;
END pack;

  
저장 프로시저:
create or replace procedure cutpage(
      -----------------------------------------
      --name:     
      --description:     
      --version: v1.0
      --created date: 2008-10-07
      --author:
      --last updated:
      --last updated by:
      -----------------------------------------
  Psql        in     varchar2,        --     SQL        ,       
  Psize       in     number,          --    
  CurrentPage in     number,          --    
  ProwCount   in out number,          --   :  
  PageCount   out    number,          --      
  Rcursor     out    pack.cur
)
as
  v_sql          varchar2(4000);      --  sql  
  v_currentpage  number;              --    
  v_Plow         number;              --      
  v_Phei         number;              --      
  v_Psize        number;

begin
  v_sql :=Psql;
  if(v_sql is null) then
      return;
  end if;
  -------------------------------------------    
  if CurrentPage is null then
     v_currentpage := 1;
  else
     v_currentpage := CurrentPage;
  end if;

  if Psize is null then
     v_Psize :=10;
  else
     v_Psize:=Psize;
  end if;
  
  -------------------------------------------           
  if ProwCount is null then
      v_sql := 'select count(*) from (' || Psql || ')';
      execute immediate v_sql into ProwCount;
      PageCount := ceil(ProwCount/v_Psize);
  else
      PageCount := ceil(ProwCount/v_Psize);
  end if;
  -------------------------------------------          
  
  if v_currentpage = 1 then
      v_sql := 'select * from ('||Psql||') 
                where rownum <= '||v_Psize||'';
  else
      v_Phei := v_currentpage * v_Psize;
      v_Plow := v_Phei - v_Psize + 1;
      v_sql := 'select *  from ( select  row_.*, rownum rownum_ 
        from ('||Psql||') row_ 
        where rownum <= ' || v_Phei || ' ) where rownum_ > ' || v_Plow || '';
  end if;
  
  --dbms_output.put_line(v_sql);
  -------------------------------------------    
  open rCursor for v_sql;

  Exception
        when others then
            --    
            rollback;
end cutpage;
 

            :

 

create or replace procedure cutpage2(
      -----------------------------------------
      --name:     
      --description:     
      --version: v1.0
      --created date: 2008-10-07
      --author:
      --last updated:
      --last updated by:
      -----------------------------------------
  Psql        in     varchar2,        --     SQL  
  Psize       in     number,          --    
  CurrentPage in     number,          --    
  Pfield      in     varchar2,        --    
  Porder      in     varchar2,        --    
  ProwCount   in out number,          --   :  
  PageCount   out    number,          --      
  Rcursor     out    pack.cur
)
as
  v_sql          varchar2(4000);      --  sql  
  v_currentpage  number;              --    
  v_porder       varchar2(50);        --    
  v_rowcount     number;              --  
  v_Plow         number;              --      
  v_Phei         number;              --      
  v_Psize        number;
  v_order        varchar2(100):='';

begin
  v_sql :=Psql;
  if(v_sql is null) then
      return;
  end if;
  -------------------------------------------    
  if CurrentPage is null then
     v_currentpage := 1;
  else
     v_currentpage := CurrentPage;
  end if;

  v_porder := Porder;
  v_rowcount := ProwCount;

  if Psize is null then
     v_Psize :=15;
  else
     v_Psize:=Psize;
  end if;

  if v_porder is null then
     v_porder := 'asc';
  end if;
  
  if Pfield is not null then
      v_order := 'order by '||Pfield||' '||v_porder||'';
  end if;
  
  -------------------------------------------           
  if v_rowcount is null then
      v_sql := 'select count(*) from (' || Psql || ')';
      execute immediate v_sql into ProwCount;
      PageCount := ceil(ProwCount/v_Psize);
  else
      PageCount := ceil(v_rowcount/v_Psize);
  end if;
  -------------------------------------------          
  
  if v_currentpage = 1 then
      v_sql := 'select * from ('||Psql||' '||v_order||') 
                where rownum <= '||v_Psize||'';
  else
      v_Phei := v_currentpage * v_Psize;
      v_Plow := v_Phei - v_Psize + 1;
      v_sql := 'select *  from ( select  row_.*, rownum rownum_ 
        from ('||Psql||' '||v_order||') row_ 
        where rownum <= ' || v_Phei || ' ) where rownum_ > ' || v_Plow || '';
  end if;
  
  --dbms_output.put_line(v_sql);
  -------------------------------------------    
  open rCursor for v_sql;

  Exception
        when others then
            --    
            rollback;
end cutpage2;


좋은 웹페이지 즐겨찾기