Table data created for the usage of the for XML path
Take a look at the initial usage of the FOR XML path: The Select Classid,sex,age,name from #PersonInfo for XML path (' PersonInfo ') can be seen from the name, FOR XML!!!
The feeling is usually used in conjunction with the bad group by. Examples are as follows:
--Statistics of male students over 22 years of age in each class select Classid,count (1) as ' number of over 22 years ', (select Name+ ', ' from #PersonInfo where Classid=p.classid for XML Path (")) as ' name set ' from #PersonInfo p where sex= ' man ' and age>22 Group by ClassID ORDER by Classidselect Classid,age , COUNT (1) as ' more than 22 years old ', (select Name+ ', ' from #PersonInfo where Classid=p.classid and age=p.age for XML Path (")) as ' name collection ' From #PersonInfo p where sex= ' man ' and age>22 Group by Classid,age ORDER by Classidselect Classid,age,count (1) as ' more than 22 years old Number ', (select Name+ ', ' from #PersonInfo where Classid=p.classid and age=p.age for XML Path (")) as ' name set ' from #PersonInfo p Where sex= ' Men ' GROUP by Classid,age have age>22 order by ClassID
The results are as follows:
The feeling of having the use is not as big as it is imagined. It is only used after group by, and it is the aggregate function that is of great use, otherwise it can be written directly in the back. Add: The comma at the back of the name set can be removed by the stuff function.
For XML Path