Solved

Nested FOR XML in SQL Server 2005

Posted on 2010-11-18
4
614 Views
Last Modified: 2012-05-10
I'm struggling to use FOR XML to create a desired layout of XML.
The problem comes in creating sibling elements with the same element names.
Within:
<message>
 <product>
  <descriptiondata>

...I need one of each elements <Brand> and <ItemType> derived from distinct rows from one table.
I also need multiple sibling elements <BulletPoint> derived from all rows of another table.


- The file "right.xml" shows what I want.

- The attached code snippet is SQL code which will generate temp tables and do what I currently do.

- The file "wrong.xml" is the XML that I currently produce (with the "sibling" items appearing in the wrong place).


How can I get the desired result?

NOTE - the example given has been simplified down from the actual one.

DECLARE @BulletPoints TABLE
(
  BulletPoint varchar(500)
)
INSERT INTO @BulletPoints SELECT '12 month guarantee'
INSERT INTO @BulletPoints SELECT 'Works straight out of the box (just fit the supplied batteries)'
INSERT INTO @BulletPoints SELECT 'No programming or setup required'

DECLARE @Stock TABLE
(
  ID int,
  ean varchar(13),
  partcode varchar(50)
)
INSERT INTO @Stock VALUES (1, '1231231231231', 'PART_001')
INSERT INTO @Stock VALUES (2, '1231231231232', 'PART_002')
INSERT INTO @Stock VALUES (3, '1231231231233', 'PART_003')




SELECT
	'1.01' AS [Header/DocumentVersion],
	'Product' AS [MessageType],
	(SELECT
	ID As "MessageID",
	partcode AS "Product/SKU",
	'EAN' As "Product/StandardProductID/Type",
	ean As "Product/StandardProductID/Value",
	'Sony' As "Product/DescriptionData/Brand",

	(SELECT BulletPoint As "BulletPoint" FROM @BulletPoints FOR XML PATH(''), type ),

	'ConsumerElectronics' As "Product/DescriptionData/ItemType"
	FROM @Stock FOR XML PATH('Message'), type)
FOR XML PATH('AmazonEnvelope'), type

Open in new window

wrong.xml
right.xml
0
Comment
Question by:Beamson
  • 3
4 Comments
 
LVL 6

Expert Comment

by:subhashpunia
ID: 34163010
0
 
LVL 1

Author Comment

by:Beamson
ID: 34163070
I'm afraid not.

The creating XML article shows how you can create DIFFERENT <color1>, <color2> etc elements in one common container element <colors>.

I need to create multiple elements with THE SAME element name at the same level in the heirarchy as other elements.

The reading XML article is not relevant to this problem (I'm trying to write it, not read it ! )
0
 
LVL 1

Accepted Solution

by:
Beamson earned 0 total points
ID: 34164230
Solved it!

The central nested FOR XML needs to have the final element name (BulletPoint) in the As part of the SELECT statement and the higher element parent names (Product/DescriptionData) in the AS for the whole statement.
Substituting the line below into the middle of my code solves the problem:

(SELECT BulletPoint As "BulletPoint" FROM @BulletSection FOR XML PATH(''), type ) As "Product/DescriptionData",

0
 
LVL 1

Author Closing Comment

by:Beamson
ID: 34194926
This solves the problem.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question