Traversing a tree structure using recursion -


I am working with a table on the database. Columns are: ID, name, parent_id, child (Parent_id will be an id on the table. If parent_id is the tap, record is a parent.)

loop through this data What would be the best way to do this, so that I would add it to the list:

- item1
- item2
& gt; ; & Gt; Item 2.1
& gt; & Gt; & Gt; & Gt; Item 2.1.1
& gt; & Gt; Item 2.2
- item3

etc.

Edit - When I say "List" I mean list

edit2 - Whoops, yes, alphabetically Sort by parental record is required. I am using mssql.

SQL Server 2008 allows you to define hierarchies in the table:

  • You can also use recurring CTE:



  • Comments