describes the SQL statement very Classic , which The SQL statement implements the descendant tree query , and the SQL statement can be directly in the Query in the parser for your reference.
- --generate table
- Create table menu (id int,mname char (), parent int)
-  
- --insert data
- inse rt into menu
- select 1, ' News ', Null union all
- select 2, ' real estate ', Null union all
- select 3, ' Science and technology News ', 1 union all
- select 4, ' social News ', 1 union all
- select 5, ' It News ', 3 union all
- select 6, ' Aerospace News ',3
-
- -- implement Query the news descendants tree
- Declare @s varchar (1000)
- select @s= ', ' +cast (Id as varchar) + ' from menu where id =1
-
- while @ @rowCount>0
-
- --charindex: Returns the starting position of the specified expression in the string
- ; select @s=@s+ ', ' +cast (Id as varchar) from MENU &NBSP
- where charindex (', ' +cast (Id as varchar) + ', ', @s+ ', ') =0
-
- and CHARINDEX (', ' +cast (Parent as varchar) + ', ', @s+ ', ') >0
-
-  
- select * from menu where charindex (', ' +cast (Id as varchar) + ', ', @s+ ', ') >0
-  
- --delete table
-  
- drop tabl E menu