0

I am dealing with one table on the database. The columns are: id, name, parent_id, hasChildren. (parent_id would be an id on the table. if parent_id is null, the record is a parent.)

What would be the best way to loop through this data so I can add it to a list like:

- item1
- item2
>> item 2.1
>>>> item 2.1.1
>> item 2.2
- item3

etc etc.

edit - when i say "list" i mean like a listbox

edit2 - woops, yes, list needs to be sorted by parent record alphabetically. i'm using mssql.

recl
  • 1
  • 1

1 Answers1

0

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

You could also use a recursive CTE:

Community
  • 1
  • 1
Johann Blais
  • 9,389
  • 6
  • 45
  • 65