mysql儲存過程實現遞迴查詢

2021-08-18 23:03:26 字數 3870 閱讀 1165

-- 建表

create table _organization(

orgcode varchar(40) not null primary key comment '機構編號',

parentcode varchar(40) comment '父機構編號',

orgname varchar(100) comment '機構名稱' ,

banktypeid varchar(10) comment '行別**' , 

orgnumber varchar(10) comment '機構號碼'

) comment = '機構資訊表' ;

-- 插入資料

insert into _organization values 

('c1010413004153', '' ,'中國銀行股份****燕郊分行'       ,'004'       ,'0520' ),

('c1010413004006'       ,'c1010413004153'       ,'中國銀行股份****三河支行'  ,'004'  ,'0519' ),

('c1010413004017'       ,'c1010413004006'       ,'中國銀行股份****三河迎賓北路支行'  ,'004'  ,'0532' ),

('c1010413004039'       ,'c1010413004006'       ,'中國銀行股份****三河世紀花苑支行'  ,'004'  ,'0534' ),

('c1010413004120'       ,'c1010413004153'       ,'中國銀行廊坊市燕郊開發區北蔡路支行'  ,'004'  ,'0526' ),

('c1010413004175'       ,'c1010413004153'       ,'中國銀行股份****廊坊市燕郊開發區迎賓路支行'  ,'004'  ,'0524' ),

('c1010413004186'       ,'c1010413004153'       ,'中國銀行股份****廊坊市燕郊開發區海油大街支行'  ,'004'  ,'0527' ),

('c1010413005195'       ,'c1010413004153'       ,'中國銀行股份****廊坊市燕郊燕順支行'  ,'004'  ,'0528' ),

('c1010413004164'       ,'c1010413004153'       ,'中國銀行股份****廊坊市燕郊開發區市場街支行'  ,'004'  ,'0525' ),

('c1010413005184'       ,'c1010413004153'       ,'中國銀行股份****廊坊市燕郊燕高支行'  ,'004'  ,'0529' ),

('c1010413004051'       ,'c1010413004040'       ,'中國銀行股份****香河迎賓路支行'  ,'004','' ),

('c1010413004062'       ,'c1010413004040'       ,'中國銀行股份****香河第一城支行'  ,'004','' ),

('c1010413004084'       ,'c1010413004073'       ,'中國銀行股份****大廠大安街支行'  ,'004','' ),

('c1010413004028'       ,'c1010413004006'       ,'中國銀行股份****三河泃陽西大街支行'  ,'004'  ,'0533' ),

('c1010413004073'       ,'c1010413004153'       ,'中國銀行股份****大廠支行'  ,'004','' ),

('c1010413004040'       ,'c1010413004153'       ,'中國銀行股份****香河支行'  ,'004' ,'' );

-- 建立儲存過程

-- 實現功能:查詢出機構編號為orgcode的所有下級機構(遞迴,利用orgcode、parentcode的關係進行遞迴查詢)

-- 建立名為「allchildorganization」 的儲存過程

create procedure allchildorganization(

in orgcode varchar(40)  -- 輸入引數為 orgcode  型別為:varchar(40)

)begin

declare p_done tinyint unsigned default(0); -- 下文中while迴圈結束的標誌

declare p_depth smallint unsigned default(0); -- 記錄查詢的深度(迴圈的次數)

-- 建立臨時表resulttemp,用於臨時存放查詢出的子孫級的機構資訊

create temporary table resulttemp( 

parentcode varchar(40) ,

orgcode varchar(40) ,

depth smallint unsigned

) engine = memory;

insert into resulttemp values( null , orgcode , p_depth ); -- 先將 輸入的orgcode 最為結點的最高端,插入到臨時表 resulttemp中

-- 建立第二個臨時表temptab,用於存放子級或者孫級(同一級)的機構資訊,然後用該臨時表,去查詢出下一級的機構資訊

create temporary table temptab engine = memory select * from resulttemp; 

while p_done <> 1 do  -- 迴圈開始

if exists(  -- 判斷機構表_organization , 是否有相應的下級機構

select 1 from _organization org , temptab  where  org.parentcode = temptab.orgcode 

) then

-- 根據臨時表temptab 查詢出相應的下級機構資訊,插入臨時表resulttemp中

insert into resulttemp select org.parentcode , org.orgcode , p_depth + 1 from _organization org , temptab where org.parentcode = temptab.orgcode and temptab.depth = p_depth;

set p_depth = p_depth + 1; 

truncate table temptab; -- 清空臨時表temptab

insert into temptab select * from resulttemp where depth = p_depth; -- 將剛查詢出的子級機構資訊,插入臨時表temptab中

else

set p_done = 1;

end if;

end while;

select org.orgcode , org.parentcode , org.orgname , org.banktypeid , org.orgnumber from _organization org , resulttemp re where org.orgcode = re.orgcode;

-- 刪除臨時表

drop temporary table if exists resulttemp; 

drop temporary table if exists temptab;

end-- 呼叫儲存過程

call allchildorganization( 'c1010413004153');

顯示出的結果:

mysql儲存過程系列一 遞迴查詢

專案中需要用到遞迴查詢的功能,所以研究了一下。首先表的結構如下 table tb department field type comment idbigint 20 not null 機構編號 seq no varchar 100 null 機構編碼 規則 省級機構 1 9,市級 10 99,縣級1...

MySQL儲存過程遞迴呼叫

有分類表tb system category,結構如下 create table tb system category id int 11 not null auto increment,c parent id int 11 not null,c name varchar 50 not null,c...

mysql儲存過程使用遞迴

實現功能為查詢節點所有子節點同時更新子節點資料,用遞迴進行處理。測試時始終報錯 error code 1456 set max sp recursion depth 100 下面是mysql遞迴呼叫的源 delimiter in uid varchar 225 in ncount int begin...