SQL Server FOR XML (Path,raw,auto,root)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.