Solved

Multiple Nodes Sql -> XML

Posted on 2012-04-06
2
775 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

717 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