利用oracle預存程序產生樹編碼,oracle預存程序

來源:互聯網
上載者:User

利用oracle預存程序產生樹編碼,oracle預存程序
利用oracle預存程序產生樹編碼需求

欄位

描述

備忘

ID

主鍵,32位UUID

 

TYPE_CODE

編碼

如:1-01-003

PARENT_ID

父節點ID,32位UUID

 

SORT_NUM

排序編號

正整數

假設頂級節點的TYPE_CODE為字元1,寫預存程序把表中所有的節點TYPE_CODE產生好;

二級節點前面補一個齡,三級補兩個零,依次類推;

實現關鍵點

n  不知道系統有多少層級,需要遞迴調用

通過遞迴調用自身;

n  如何動態在TYPE_CODE前面填充‘0’;通過計算‘-’的個數來確定層級,從而確定首碼的個數

tree_level:= (length(p_code)-length(replace(p_code,'-',''))) + 1;

 

n  前面填充首碼‘0’字元

lpad(to_char(cnt),tree_level,'0')

 

預存程序代碼
CREATEOR REPLACE PROCEDURE INI_TREE_CODE(  V_PARENT_ID IN VARCHAR2 )AS  p_id  varchar2(32);  p_code varchar2(256);   sub_num   number(4,0);  tree_level number(4,0);   cnt       number(4,0) default 0;   cursor treeCur(oid varchar2) is  select id,TYPE_CODE from eval_index_type  where parent_id = oid  order by sort_num;   BEGIN  sub_num := 0;   select id,type_code into p_id,p_code  from eval_index_type  where id = V_PARENT_ID  order by sort_num;   for curRow in treeCur(p_id) loop    cnt := cnt +1;    tree_level :=(length(p_code)-length(replace(p_code,'-',''))) + 1;       update eval_index_type set type_code =p_code || '-' || lpad(to_char(cnt) ,tree_level,'0')    where id = curRow.id;       select COUNT(*) into sub_num fromeval_index_type where parent_id = p_id;     if sub_num > 0 then      INI_TREE_CODE (curRow.id);    end if;   end loop;ENDINI_TREE_CODE;


相關文章

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.