Solved

Multiple Nodes Sql -> XML

Posted on 2012-04-06
2
746 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 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

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

Suggested Solutions

Title # Comments Views Activity
Insert data into database 2 44
Excel - SQL export question 3 41
mysql disables rename 4 70
paypal ipn to mysql 3 38
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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