Wednesday, December 30, 2009

Get hierarchical XML data in SqlServer

Select a.EmployeeID as '@EmployeeID', a.EmployeeName as '@EmployeeName',a.DOJ as 'DateOfJoining'
(
Select
b.ProjectID as '@ProjectID',
b.ProjectName as '@ProjectName' ,
b.StartDate as '@StartDate' from Projects b where a.EmployeeID = b.EmployeeID for xml path('Project'), type
) as 'Projects'
from Employee a
FOR xml PATH('Employee') , ROOT

It would give output like this:-















No comments:

Post a Comment