Extract Data from XML using SQL server 2005

Having difficulty to extract data from XML document I am using .value method but it extracts only a single piece of information from XML document.
How do I extract all the values from xml? Any help would be much appreciated...
DECLARE @XmlDocument xml
 
SET @XmlDocument = N'<report>
  <d-report>
    <summary>
      <d-nbr>200700606058</d-nbr>
      <d-ver>17</d-ver>      
    </summary>
  </d-report>
  <d-report>
    <summary>
      <d-nbr>200600925766</d-nbr>
      <d-ver>10</d-ver>      
    </summary>
  </d-report>
  <d-report>
    <summary>
      <d-nbr>200600917443</d-nbr>
      <d-ver>13</d-ver>
    </summary>
  </d-report>
  <d-report>
    <summary>
      <d-nbr>200600913074</d-nbr>
      <d-ver>30</d-ver>
    </summary>
  </d-report>  
</report>'
 
SELECT @XmlDocument.value('(/report/d-report/summary/d-nbr)[1]', 'nvarchar(50)' ) [d-nbr],
@XmlDocument.value('(/report/d-report/summary/d-ver)[1]', 'nvarchar(50)' ) [d-ver]

Open in new window

andrishelpAsked:
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.

Aneesh RetnakaranDatabase AdministratorCommented:
SELECT ParamValues.ID.value('d-nbr[1]','VARCHAR(20)') [d-nbr],
       ParamValues.ID.value('d-ver[1]','VARCHAR(20)')[d-ver]
FROM @XmlDocument.nodes('/report/d-report/summary') as ParamValues(ID)
0
andrishelpAuthor Commented:
Thanks for your quick response. I have changed few things in the XML Document. Now I am not getting correct result set. Please check attach file. Thanks.
XML-SQL.doc
0
pepepacoCommented:
change your select query for

SELECT cust.coldef.value('(d-nbr)[1]', 'nvarchar(50)' ) [d-nbr],
cust.coldef.value('(d-ver)[1]', 'nvarchar(50)' ) [d-ver]
from @strXML.nodes('/report/d-report/summary') AS cust(coldef)


regards.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

pepepacoCommented:
i forgot to mention i changed the xml variable from @XmlDocument  to @strXML please take into account.
you can switch the name back if you want.
regards.
0
Anthony PerkinsCommented:
Since you do not have a primary key, the best you can do is:

SELECT      [d-nbr],
      MIN([d-ver]) [d-ver],
      MIN([p-title]) [p-title]
FROM      (
    SELECT  ParamValues.ID.value('d-nbr[1]', 'VARCHAR(20)') [d-nbr],
          ParamValues.ID.value('d-ver[1]', 'VARCHAR(20)') [d-ver],
          project.ID.value('title[1]', 'nvarchar(500)') [p-title]
    FROM    @XmlDocument.nodes('/report/d-report/summary') ParamValues (ID),
          @XmlDocument.nodes('/report/d-report/data/proj-title/title-code') project (ID)) d
GROUP BY [d-nbr]
0
andrishelpAuthor Commented:
After modifying xml file, getting duplicate records. Could you please review the attach file that I have sent eariler? Thanks.
0
pepepacoCommented:
do this work for you??


SELECT ParamValues.ID.value('(summary/d-nbr)[1]','VARCHAR(20)') [d-nbr]
,ParamValues.ID.value('(summary/d-ver)[1]','VARCHAR(20)') [d-ver]
,ParamValues.ID.value('(data/proj-title/title-code/title)[1]','VARCHAR(20)') [title1]
,ParamValues.ID.value('(data/proj-title/title-code/title)[2]','VARCHAR(20)') [title2]
FROM @XmlDocument.nodes('/report/d-report') as ParamValues(ID)


it works only if there are always 2 titles at max...

regards
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
pepepacoCommented:
other way to do it exactly as you need it is :

select * from (
SELECT ParamValues.ID.value('(summary/d-nbr)[1]','VARCHAR(20)') [d-nbr]
,ParamValues.ID.value('(summary/d-ver)[1]','VARCHAR(20)') [d-ver]
,ParamValues.ID.value('(data/proj-title/title-code/title)[1]','VARCHAR(20)') [title1]
FROM @XmlDocument.nodes('/report/d-report') as ParamValues(ID)
union all
SELECT ParamValues.ID.value('(summary/d-nbr)[1]','VARCHAR(20)') [d-nbr]
,ParamValues.ID.value('(summary/d-ver)[1]','VARCHAR(20)') [d-ver]
,ParamValues.ID.value('(data/proj-title/title-code/title)[2]','VARCHAR(20)') [title1]
FROM @XmlDocument.nodes('/report/d-report') as ParamValues(ID))sub
order by [d-nbr],[d-ver],[title1]


0
Aneesh RetnakaranDatabase AdministratorCommented:
I thought I answered the original question :(
0
Anthony PerkinsCommented:
It is what you call a moving target...
0
pepepacoCommented:
I think I answered without any assistance!
.
..
...
 I'm just kidding.  :)


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 Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.