SQL递归函数
生成一张表,添加数据 DROP table IF EXISTS Dept ; create table Dept ( ID int , ParentID int , msg varchar ( 20 ) ) insert into Dept select 1 , 0 , '中国' insert into Dept select 2 , 1 , '上海' insert into Dept select 3 , 1 , '浙江' insert into Dept select 4 , 2 , '普陀区' insert into Dept select 5 , 3 , '杭州' insert into Dept select 6 , 5 , '西湖区' insert into Dept select 7 , 6 , '双浦镇' go 递归函数: Create function GetChild ( @ID varchar ( 10 ) ) returns @t table ( ID varchar ( 10 ) , ParentID varchar ( 10 ) , Level int , msg varchar ( 20 ) ) as begin declare @i int set @i = 1 --insert into @t select @ID,@ID,0 --当前级,本级