Oracle 函數中遊標及遞迴的應用

來源:互聯網
上載者:User

Oracle 函數中遊標及遞迴的應用

在代碼中使用遞迴可能大部分程式員都不陌生,但是在預存程序或者函數中寫個遞迴估計就不多見了。

今天遇到了一個在Oracle函數中使用遞迴的例子,特記錄下來,方便以後查閱

CREATE OR REPLACE FUNCTION F_Func(v_pcatalogue IN NUMBER, i_newPcatalogue IN NUMBER, v_authTypeId in number)
  RETURN NUMBER
  as
  v_newCatalogueId number(10);
  v_oldCatalogueId number(10);
  v_newPcatalogue number(10);
  v_count number(10);
  v_value number(10);
  --根據目錄表父節點ID查詢目錄表資料的遊標
  Cursor cusor_1(v_pcatalogue number) is
        select id, cname, ename, cdescription, edescription, authtype, pcatalogue, orderb, createtime, userid, creater, updatetime, updateuserid, updator, state from t_catalogue
        where authtype=v_authTypeId and pcatalogue=v_pcatalogue;

BEGIN
  --記錄下舊的父節點ID
  v_oldCatalogueId := v_pcatalogue;
  --記錄下新的父節點ID
  v_newPcatalogue := i_newPcatalogue;
  select count(1) into v_count from t_catalogue where authtype=v_authTypeId and pcatalogue=v_pcatalogue;
  if v_count = 0 then
    return 1;
  else
    for c2 in cusor_1(v_oldCatalogueId) loop
 --產生新的id及拷貝工作
      v_oldCatalogueId := c2.id;
      select HIBERNATE_SEQUENCE.NEXTVAL into v_newCatalogueId from dual;
      if v_newPcatalogue is null then
        select HIBERNATE_SEQUENCE.NEXTVAL into v_newPcatalogue from dual;
      end if;
      insert into t_catalogue_20140619(id, cname, ename, cdescription, edescription, authtype, pcatalogue, orderb, createtime, userid, creater, updatetime, updateuserid, updator, state)
            values(v_newCatalogueId, c2.cname, c2.ename, c2.cdescription, c2.edescription, c2.authtype, v_newPcatalogue, c2.orderb, c2.createtime, c2.userid, c2.creater, c2.updatetime, c2.updateuserid, c2.updator, c2.state);
      commit;
      --不用變數接值,編譯過不了
      v_value := F_Func(v_oldCatalogueId, v_newCatalogueId, v_authTypeId);
    end loop;
    --這個返回很重要
    return 1;
  end if;

END F_Func;
/

Linux-6-64下安裝Oracle 12C筆記

在CentOS 6.4下安裝Oracle 11gR2(x64)

Oracle 11gR2 在VMWare虛擬機器中安裝步驟

Debian 下 安裝 Oracle 11g XE R2

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.