开发者

Traversing a tree structure using recursion

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:

开发者_StackOverflow中文版

- 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.


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

  • http://msdn.microsoft.com/en-us/magazine/cc794278.aspx
  • Question about SQL Server HierarchyID depth-first performance

You could also use a recursive CTE:

  • Determine the hierarchy of records in a SQL database
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜