Saturday, 23 March 2013

XML Generation using SQL Query and Formating

Source Table

                           Result : XML Generation using Sql query and Formatting





There are some key words to generate xml at end of the query, one of them in 'for xml auto '

Format Tips using 'for xml auto'

1.This will take table name as node name.
2.Create nodes as much table you joined in the query.
3.Columns of the table you mention in query will come as attribute.

simple example query is below for above out put




      Select 
Students.[Student] as Name
,Exams.[Exams] as Exam
,Scores.Subject as Subject
,Scores.Marks as Marks
  from 
StudentDetails as Students
join 
StudentDetails as Exams
      on
Exams.Id = Students.Id
join 
StudentDetails as Scores
    on
Scores.Id = Students.Id

  for xml auto 


Output:


                         

No comments:

Post a Comment