create table Depart(
Id int(10) not null auto_increment primary key,
Parent char(20),
Child char(20)
)
insert into Depart(Parent,Child) values ('a','b')
insert into Depart(Parent,Child) values ('b','c')
insert into Depart(Parent,Child) values ('c','d')
drop procedure if exists Pro_Sel;
create procedure Pro_Sel(in p_Child varchar(100))
begin
declare tmpChild varchar(100) default NULL;
drop table if exists t_table;
create temporary table t_table (field1 varchar(100));
set tmpChild = p_Child;
insert into t_table (field1) values (tmpChild);
while_label:while 1 DO
select Parent from Depart where Child = tmpChild into @t_tmp;
if CHAR_LENGTH(@t_tmp) <= 0 OR @t_tmp is null then
leave while_label;
end if;
insert into t_table (field1) values (@t_tmp);
set tmpChild = @t_tmp;
set @t_tmp = '';
end while;
select * from t_table;
end
call Pro_Sel("d")
用存储过程写的一个小递归程序
最新推荐文章于 2024-02-01 14:01:32 发布