Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multiple Nodes Sql -> XML

Posted on 2012-04-06
2
Medium Priority
?
804 Views
Last Modified: 2012-06-27
I'm trying to add multiple nodes with the same fields in them and SQL isn't too happy about that.

What I want is
<ADM>
   <Coverage_Loss>
      <Coverage id="COV1" />
      <Coverage id="COV2"/>
      <Coverage id="COV3"/>
   </Coverage_Loss>
</ADM>
etc.

I have this (I omitted some irrelevant fields):
Select 'COV1' as 'ADM/COVERAGE_LOSS/COVERAGES/COVERAGE/@id'
         ,'COV2' as 'ADM/COVERAGE_LOSS/COVERAGES/COVERAGE/@id'
         ,'COV3' as 'ADM/COVERAGE_LOSS/COVERAGES/COVERAGE/@id'
FROM tablesblah
FOR XML PATH ('XMLPATHHERE')

Open in new window


But I get the error:
Column name 'ADM/COVERAGE_LOSS/COVERAGES/COVERAGE/@id' is repeated. The same attribute cannot be generated more than once on the same XML tag.

Is there a way to get the multiple nodes with the same names working here?
0
Comment
Question by:Josh_Blade
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 37817054
You need to include a sub-query to return COVERAGE as a set of rows.  Something like below.  But I'm not sure how that fits into your query as you would need to have your column values as rows (you could use a pivot query for that).
Select 
    (Select COVERAGE as 'COVERAGE/@id'
    From
    (
        select 'COV1' COVERAGE
        union
        select 'COV2' COVERAGE
        union
        select 'COV3' COVERAGE
    ) a For XML PATH(''), type) as 'ADM/COVERAGE_LOSS/COVERAGES'
--FROM tablesblah
FOR XML PATH ('')

/*
Output:

<ADM>
  <COVERAGE_LOSS>
    <COVERAGES>
      <COVERAGE id="COV1" />
      <COVERAGE id="COV2" />
      <COVERAGE id="COV3" />
    </COVERAGES>
  </COVERAGE_LOSS>
</ADM>

*/

Open in new window

0
 

Author Closing Comment

by:Josh_Blade
ID: 37817156
Thanks for the help. That looks like it will do it. I just need to hammer out the details now.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

783 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