For XML Path

Source: Internet
Author: User

/*

 

For XML Path

We have already discussed the use of for XML (Auto, raw). The following describes the use of the for XML Path mode.

The auto and raw modes can meet most XML format requirements. The path mode can provide other format functions.

*/

 

-- 1. control level

/*

In the path mode, you can generate the XML node hierarchy. The XML hierarchy is controlled by column aliases.

The following query adds a new node info that includes name, phone, and sex. Note that "/" in the alias "/"

Is used to control the XML generation level.

*/

 

-- The test table follows the test table employees in for XML (Auto, raw.

 

Select

Employeeid as '@ id ',

Name as 'info/name ',

Phone As 'info/phone ',

Sex as 'info/sex'

From employees for XML Path ('Employee'), type, elements, root ('ployees ')

 

/*

<Employees>

<Employee ID = "1">

<Info>

<Name> Tudou </Name>

<Phone> 15958944683 </phone>

<Sex> male </sex>

</INFO>

</Employee>

<Employee ID = "2">

<Info>

<Name> shuige </Name>

<Phone> 00000000000 </phone>

<Sex> male </sex>

</INFO>

</Employee>

<Employee ID = "3">

<Info>

<Name> Xiaozhu </Name>

<Phone> 11111111111 </phone>

<Sex> male </sex>

</INFO>

</Employee>

</Employees>

*/

 

-- 2: generate a list

 

/*

We can see that the path can be used to control the hierarchy, and the path can also be used to generate a list.

It is useful when performing column conversion. You should often see this usage when visiting csdn. Below

Generate a list of employeeids

*/

 

 

 

Select

Employeeid as 'data ()'

From employees

For XML Path ('')

 

/*

Result:

1 2 3

*/

 

/*

We can see that the results are separated by spaces, but most of the time we need to use certain symbols.

Separated by commas.

*/

 

Select Replace (

(Select

Employeeid as 'data ()'

From employees

For XML Path ('')),'',',')

 

/*

Result:

1, 2, 3

*/

For XML Path

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.