tsql - How do I retrieve hierarchic XML in t-sql? -


my table has following schema:

id, parent_id, text

given following data return xml hierarchy:

data: (1,null,'x'), (2,1,'y'), (3,1,'z'), (4,2,'a')

xml:
[row text="x"]
[row text="y"]
[row text="a"/]
[/row]
[row text="z"/]
[/row]


added: hierachy has no maximum depth

if have finite depth there's quickie looks this:

select t.*, t2.*, t3.* /*, ...*/ mytable t  inner join mytable t2 on t2.parent_id=t.id inner join mytable t3 on t3.parent_id=t2.id /* ... */ t.parent_id null xml auto 

i'm not sure might possible devise similar result using recursive queries. of course, it's easier (and makes more sense) in application level.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -