MySQL儲存過程遞迴呼叫
阿新 • • 發佈:2019-01-24
有分類表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_full_name` varchar(200) DEFAULT NULL, `c_code` varchar(50) NOT NULL, `c_describe` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=126 DEFAULT CHARSET=utf8;
要求使用儲存過程“根據父分類代號(c_code)取得所有子分類及孫子分類”。
使用以下儲存過程:
1. 主儲存過程,作用是建立臨時表,並操作其他儲存過程或函式實現需求,其中臨時表的作用是儲存每個子分類的代號。流程:建立臨時表——呼叫儲存過程(category_findCodesByParentCode_queryAndInsert)取得所有子分類及孫子分類的程式碼並存入臨時表中——呼叫函式(category_generateResult)生成結果字串——刪除臨時表資料——返回生成的字串。
2. 遞迴取得所有子分類及孫子分類並存儲到臨時表中。流程:根據父分類代號查詢下級子分類代號,並通過指標迭代之——在迭代過程中,將子分類的代號存入臨時表——呼叫函式(category_findChildrenCountByCode)檢查子分類是否有下級分類,若無不管之;若有則遞迴呼叫儲存過程(category_findCodesByParentCode_queryAndInsert)取得孫子分類。CREATE PROCEDURE category_findCodesByParentCode(in cCode varchar(200)) begin -- 呼叫的函式或儲存過程:category_findCodesByParentCode_queryAndInsert、category_generateResult -- 被調用於函式或儲存過程:無 declare cRand varchar(50) default RAND(); declare result varchar(4000); create temporary table if not exists tb_system_temp_category_categoryTree( c_result varchar(4000), c_rand varchar(50) ); set max_sp_recursion_depth = 100; call category_findCodesByParentCode_queryAndInsert_zh(cCode, cRand); set result = category_generateResult(cRand); set @mySql = CONCAT('delete from tb_system_temp_category_categoryTree where c_rand = "',cRand,'"'); prepare stmt from @mySql; execute stmt; set @mySql = CONCAT('select "', result, '" from tb_system_user limit 0,1'); prepare stmt from @mySql; execute stmt; end
3. 根據分類代號取得子分類的個數。CREATE PROCEDURE category_findCodesByParentCode_queryAndInsert(in cCode varchar(200), in cRand varchar(50)) begin -- 呼叫的函式或儲存過程:category_findChildrenCountByCode、category_findCodesByParentCode_queryAndInsert -- 被調用於函式或儲存過程:category_findCodesByParentCode declare finished int default 0; declare thisCode varchar(200); declare cur cursor for select c_code from tb_system_category where c_parent_id in (select id from tb_system_category where c_code = cCode); declare continue handler for not found set finished = 1; open cur; fetch cur into thisCode; while finished = 0 do set @mySql = CONCAT('insert into tb_system_temp_category_categoryTree(c_result,c_rand) values("',thisCode,'","',cRand,'")'); prepare stmt from @mySql; execute stmt; if category_findChildrenCountByCode(thisCode) > 0 then call category_findCodesByParentCode_queryAndInsert(thisCode, cRand); end if; fetch cur into thisCode; end while; close cur; end
CREATE FUNCTION category_findChildrenCountByCode(cCode varchar(200)) RETURNS int(11)
BEGIN
-- 呼叫的函式或儲存過程:無
-- 被調用於函式或儲存過程:category_findCodesByParentCode_queryAndInsert
declare finished int default 0;
declare count int;
declare cur cursor for select count(id) from tb_system_category where c_code like CONCAT(cCode,'%') and c_code != cCode;
declare continue handler for not found set finished = 1;
open cur;
fetch cur into count;
close cur;
if count is null then
return 0;
else
return count;
end if;
END
4. 從臨時表中查出結果並組合成字串。
CREATE FUNCTION category_generateResult(cRand varchar(50)) RETURNS varchar(4000) CHARSET utf8
BEGIN
-- 呼叫的函式或儲存過程:無
-- 被調用於函式或儲存過程:category_findCodesByParentCode
declare finished int default 0;
declare result varchar(20000) default '';
declare thisResult varchar(200) default '';
declare cur cursor for select c_result from tb_system_temp_category_categoryTree where c_rand = cRand;
declare continue handler for not found set finished = 1;
open cur;
fetch cur into thisResult;
while finished = 0 do
set result = concat(result, thisResult, ',');
fetch cur into thisResult;
end while;
close cur;
if result is null then
return result;
else
if RIGHT(result,1) = ',' then
set result = SUBSTR(result, 1, CHAR_LENGTH(result) - 1);
end if;
return result;
end if;
END
在MySQL中,不能夠直接使用函式實現遞迴,在上例中,使用儲存過程遞迴呼叫,將需要的值儲存到臨時表中,然後通過對臨時表進行操作後取得結果。