Multiple Nodes Sql -> XML

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?
Josh_BladeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wdosanjosCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Josh_BladeAuthor Commented:
Thanks for the help. That looks like it will do it. I just need to hammer out the details now.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.