瀹氫箟鍑芥暟 create function c_tree(@initid int)/*瀹氫箟鍑芥暟c_tree錛岃緭鍏ュ弬鏁頒負鍒濆鑺傜偣id*/ returns @t table(id int,name varchar(100),parentid int,lev int)/*瀹氫箟琛╰鐢ㄦ潵瀛樻斁鍙栧嚭鐨勬暟鎹?/ begin 聽 declare @i int/*鏍囧織閫掑綊綰у埆*/ 聽 set @i=1 聽 insert @t select id,name,parentid,@i from tb_city where id=@initid 聽 while @@rowcount<>0 聽 begin 聽 set @i=@i+1 聽 insert @t select a.id,a.name,a.parentid,@i from tb_city as a,@t as b 聽where b.id=a.parentid and b.lev=@i-1 聽 end return end
鎵ц 浣跨敤鍑芥暟 select * from c_tree(1) /*鍙栨箹鍖椾笅闈㈢殑瀛愯妭鐐?/
Oracle涓殑瀹炵幇
select *聽 from TB_CITY /*姝ゅ鍙啓WHERE璇彞闄愬埗*/ start with聽ID=1 connect by prior ID=PARENTID