1. FOR XML path (' str ')
Select Id,createtime from dbo. Articleinfo FOR XML Path (' MyTitle ')
Result: (Note: If it is select Id,createtime from dbo.) Articleinfo FOR XML path below results <mytitle> becomes default <row>)
<mytitle>
<ID>4</ID>
<CreateTime>2015-02-03T10:44:15.857</CreateTime>
</mytitle>
<mytitle>
<ID>5</ID>
<CreateTime>2015-02-03T14:04:45.093</CreateTime>
</mytitle>
2. FOR XML Path (")
Select ' [' +convert (Varchar,id) + '] ', ' [' +title+ '] ' from dbo. Articleinfo FOR XML Path (")
Results:
[1] [News Title 1] [2] [News Title 2]
3. FOR XML row (raw: The field is displayed as a property)
Select Id,menutype from Articleinfo FOR XML raw
Results:
<row id= "4" menutype= "6"/>
<row id= "5" menutype= "5"/>
<row id= "6" menutype= "5"/>
4. FOR XML row (' str ') (Raw: The field is displayed as a property)
Select Id,menutype from Articleinfo FOR XML raw (' MyTitle ')
Results:
<mytitle id= "4" menutype= "6"/>
<mytitle id= "5" menutype= "5"/>
<mytitle id= "6" menutype= "5"/>
5, FOR XML auto (auto: Is the table name (such as: Articleinfo) as the element name display field)
Select Id,menutype from Articleinfo FOR XML auto
<articleinfo id= "4" menutype= "6"/>
<articleinfo id= "5" menutype= "5"/>
<articleinfo id= "6" menutype= "5"/>
6. Root (' str ') Note: Used behind for XML path/raw/auto; Add root node based on query XML results
Select Id,menutype from Articleinfo for XML raw,root (' Xmlpath ')
<xmlpath>
<row id= "4" menutype= "6"/>
<row id= "5" menutype= "5"/>
<row id= "6" menutype= "5"/>
</xmlpath>
7, elements
Select Id,menutype from Articleinfo for XML raw,root (' Xmlpath '), elements
<xmlpath>
<row>
<ID>4</ID>
<MenuType>6</MenuType>
</row>
<row>
<ID>5</ID>
<MenuType>5</MenuType>
</row>
</xmlpath>
SQL Server FOR XML (Path,raw,auto,root)