Fun with T-SQL

Convert XML string to parent-child table of elements in T-SQL

Created using recursive CTEs. Run in SQL 2008.

See other SQL fractals

T-SQL Source

declare @j xml
set @j = '<root>
  <Location LocationID="10">
     <step>...</step>
     <step>...</step>
  </Location>
  <Location LocationID="20">
  </Location>
  <Location LocationID="30">
  </Location>
</root>'
;with elems(x) as
(
  select @j
  union all
  select res.elem.query('.') from elems cross apply elems.x.nodes('*/*') res(elem)
)
select x, x.value('fn:local-name(*[1])','nvarchar(max)') from elems

	

See other SQL fractals