delimiter //
DROP PROCEDURE IF EXISTS test;
CREATE PROCEDURE test()
BEGIN
DECLARE tem_id int;
declare flag int default 0;
DECLARE cnt INT DEFAULT 0;
DECLARE s_list CURSOR for SELECT id FROM system_areas where level
= 3;
declare continue handler for not found set flag=1;
open s_list; # 打开游标
fetch s_list into tem_id;
while flag <> 1 do
set @cnt = (select count(*) from system_areas WHERE parent_id = tem_id);
IF @cnt=0 THEN
#select * from system_areas where id = tem_id;
UPDATE system_areas set hasChild = 0 WHERE id = tem_id;
END IF;
fetch s_list into tem_id;
end while;
END
//
CALL test();