SQL FOR XML splicing column data of the same class

Source: Internet
Author: User

The database records the work experience of the employees, one through the data, the existing demand for Excel to export the work experience of employees, so you have to put the work experience together

The test data table is as follows

The results that need to be processed are as follows (multiple experiences are separated by commas):

SQL statements:

Select EmpID, Left (Workexperience,len (workexperience) – 1) as Workexperience from  (select EmpID, (select experience+ + ', ' from dbo. The Employee WHERE EmpID =a.empid for XML PATH (")) as workexperience from dbo. Employee A GROUP by A.empid) B

The query result of Select is returned as a rowset, but you can also specify a FOR XML clause in SQL to retrieve the query as XML. In the FOR XML clause, you can specify one of the following modes:RAW, AUTO, explicit, and path.

Raw mode:

SELECT TOP 3 id,name,clientcode from dbo. Customer for XML RAW

Results:

<row id= "94" Name= "1st Paygateway.net" clientcode= "c-na-tl-0063"/><row id= "" name= "3D NURI CO LTD" Clientcode = "c-as-tl-0049"/><row id= "" "Name=" TV "clientcode=" c-na-tl-0064 "/>

Raw mode:

SELECT TOP 3 id,name,clientcode from dbo. Customer for XML AUTO

Results:

<dbo. Customer id= "94" Name= "1st Paygateway.net" clientcode= "c-na-tl-0063"/><dbo. Customer id= "name=" 3D NURI CO LTD "clientcode=" c-as-tl-0049 "/><dbo. Customer id= "name=" clientcode= "c-na-tl-0064"/>

The relevant SQL for XML is referenced by: http://www.cnblogs.com/kenshincui/archive/2011/12/31/2309217.html

SQL FOR XML splicing column data of the same class

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.