oracle函數及包的管理方式

--但個函數進行管理

CREATE OR REPLACE function testuser.proone(shortName in varchar2)  return varchar2 is

tMaxNo2 varchar(64);

 pragma autonomous_transaction;

begin

  declare

    tMaxNo integer := 0;

    tMaxNo1 varchar(32) ;

    tMaxNo2 varchar(32) ;

    yearY varchar(2) ;

    begin

      select max(maxno) into tMaxNo from  testuser.t_maxno_conf where type='proone';

      if tMaxNo is null or tMaxNo='' then

          insert into  testuser.t_maxno_conf(type,maxno)values('proone',1);

           commit;

          tMaxNo:=1;

          select lpad('1','2',0) into tMaxNo1 from dual;

        tMaxNo2:='HK-'||yearY||'-'||shortName||'-'||tMaxNo1;

      else

      update  testuser.t_maxno_conf a set a.maxno=a.maxno+1  where a.type='proone';

      commit;

      select lpad(tMaxNo,'2',0) into tMaxNo1 from dual;

      select SUBSTR(extract(year from sysdate),3,4) into yearY from dual;

      tMaxNo2:='HK-'||yearY||'-'||shortName||'-'||tMaxNo1;

      END IF;

   return tMaxNo2;

   end;

end proone;  --不需要加屬主,否則報錯

/

 

包的方式進行函數管理

包頭

create or replace package testuser.testuser_project_package is

function proone(shortName in varchar2)  return varchar2 ;

end testuser_project_package;  --結尾不需要加屬主

/

 

包體

create or replace package body testuser.testuser_project_package  --包體開頭,可以加屬主

  is

function proone(shortName in varchar2)

return varchar2 is

   tMaxNo2 varchar(64);

   pragma autonomous_transaction;

begin

  declare

    tMaxNo integer := 0;

    tMaxNo1 varchar(32) ;

    tMaxNo2 varchar(32) ;

    yearY varchar(2) ;

    begin

      select max(maxno) into tMaxNo from testuser.t_maxno_conf where type='proone';

      if tMaxNo is null or tMaxNo='' then

          insert into  testuser.t_maxno_conf(type,maxno)values('proone',1);

          commit;

          tMaxNo:=1;

          select lpad('1','2',0) into tMaxNo1 from dual;

          tMaxNo2:='HK-'||yearY||'-'||shortName||'-'||tMaxNo1;

      else

          update testuser.t_maxno_conf a set a.maxno=a.maxno+1  where a.type='proone';

          commit;

          select lpad(tMaxNo,'2',0) into tMaxNo1 from dual;

          select SUBSTR(extract(year from sysdate),3,4) into yearY from dual;

          tMaxNo2:='HK-'||yearY||'-'||shortName||'-'||tMaxNo1;

      END IF;

      return tMaxNo2;  --每個函數體的結尾標示

   end ;

  end proone; --包體的結尾標示

 

end testuser_project_package;  --結尾不需要加屬主

/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章