CREATE PROCEDURE proEntpTypeInfo(iid int(11),lvl int) BEGIN -- 局部變數定義 declare tid int(11) default -1 ; declare ttype_name varchar(255) default '' ; declare tptype_id int(11) default -1 ; -- 遊標定義 declare cur1 CURSOR FOR select id,type_name,ptype_id from entp_type_info where (ptype_id=iid or id=iid)and type = 20 and is_del = 0; -- 遊標介紹定義 declare CONTINUE HANDLER FOR SQLSTATE '02000' SET tid = null,ttype_name=null,tptype_id=null; SET @@max_sp_recursion_depth = 13; -- 開遊標
OPEN cur1; FETCH cur1 INTO tid,ttype_name,tptype_id; WHILE ( tid is not null )
DO insert into tmp_entp_type_info values(tid,ttype_name,tptype_id,lvl); -- 樹形結構資料遞迴收集到建立的暫存資料表中 call proEntpTypeInfo(tid,lvl+1); FETCH cur1 INTO tid,ttype_name,tptype_id ; END WHILE; END; drop procedure if exists proEntpTypeInfo;
drop temporary table if exists tmp_entp_type_info; create temporary table if not exists tmp_entp_type_info(id int(20),type_name varchar(255), fid int(11),lvl int); call proEntpTypeInfo(7,0); select * from tmp_entp_type_info ; |