Solved

XML in SQL - Query

Posted on 2014-04-10
5
357 Views
Last Modified: 2014-04-18
Hi. I have a table which looks like this

ID (as uniqueidentifier) - key
Date (as datetime)
XMLFile (as XML)


The XML file is much worse composed, it is basically a snapshot of data at some point in time

It looks something like The code at the bottom but MUCH MUCH longer (yay):

As you notice, each xml file has multiple members
And the subnodes (ident) are NOT in order or sometimes missing altogether

I need to write a simple query,
The pseudocode is

SELECT  XMLFILE.value(/root//...ident/qualifier/identification (WHARE QUALIFIER = AAA))
FROM TABLE
WHERE XMLFILE.value(/root//....(Membernum = 0000001 OR 0000002  )



<root>
  <identity>0000001</identity>
    <member>
    <membernum>0000001</membernum>
    <identities>
      <ident>
        <qualifier>1</qualifier>
        <identification>0000001</identification>
      </ident>
      <ident>
        <qualifier>Name</qualifier>
        <identification>George Forman</identification>
      </ident>
      <ident>
        <qualifier>State</qualifier>
        <identification>Confusion</identification>
      </ident>
      <ident>
        <qualifier>AAA</qualifier>
        <identification>ABC123</identification>
      </ident>
    </identities>
    <member>
    <membernum>0000002</membernum>
    <identities>
      <ident>
        <qualifier>Name</qualifier>
        <identification>Alexander Bell</identification>
      </ident>
      <ident>
        <qualifier>State</qualifier>
        <identification>Dilusion</identification>
      </ident>
      <ident>
        <qualifier>AAA</qualifier>
        <identification>ABC12345</identification>
      </ident>
    </identities>
  </member>
0
Comment
Question by:Ess Kay
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39993179
That is not a well-formed Xml document.  Do you mean:
<root>
      <identity>0000001</identity>
      <member>
            <membernum>0000001</membernum>
            <identities>
                  <ident>
                  <qualifier>1</qualifier>
                  <identification>0000001</identification>
                  </ident>
                  <ident>
                  <qualifier>Name</qualifier>
                  <identification>George Forman</identification>
                  </ident>
                  <ident>
                  <qualifier>State</qualifier>
                  <identification>Confusion</identification>
                  </ident>
                  <ident>
                  <qualifier>AAA</qualifier>
                  <identification>ABC123</identification>
                  </ident>
            </identities>
     </member>
      <member>
            <membernum>0000002</membernum>
            <identities>
              <ident>
                  <qualifier>Name</qualifier>
                  <identification>Alexander Bell</identification>
              </ident>
              <ident>
                  <qualifier>State</qualifier>
                  <identification>Dilusion</identification>
              </ident>
              <ident>
                  <qualifier>AAA</qualifier>
                  <identification>ABC12345</identification>
              </ident>
            </identities>
      </member>
</root>
0
 
LVL 15

Author Comment

by:Ess Kay
ID: 39999766
yea sorry, i edited it on the fly and left those out by accident.
for needless to say reasons i cannot paste the real data
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 40000536
You can do something like this:
SELECT  t.c.value('.', 'varchar(20)')
FROM    @YourTable
        CROSS APPLY XmlFile.nodes('root/member/identities/ident/identification[../../../membernum = "0000001" or ../../../membernum = "0000002"]') T (C)

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40000542
This is how I tested it:
DECLARE @YourTable TABLE (XmlFile xml NOT NULL)

INSERT @YourTable (XmlFile)
VALUES  (
'<root>
      <identity>0000001</identity>
      <member>
            <membernum>0000001</membernum>
            <identities>
                  <ident>
                  <qualifier>1</qualifier>
                  <identification>0000001</identification>
                  </ident>
                  <ident>
                  <qualifier>Name</qualifier>
                  <identification>George Forman</identification>
                  </ident>
                  <ident>
                  <qualifier>State</qualifier>
                  <identification>Confusion</identification>
                  </ident>
                  <ident>
                  <qualifier>AAA</qualifier>
                  <identification>ABC123</identification>
                  </ident>
            </identities>
     </member>
      <member>
            <membernum>0000002</membernum>
            <identities>
              <ident>
                  <qualifier>Name</qualifier>
                  <identification>Alexander Bell</identification>
              </ident>
              <ident>
                  <qualifier>State</qualifier>
                  <identification>Dilusion</identification>
              </ident>
              <ident>
                  <qualifier>AAA</qualifier>
                  <identification>ABC12345</identification>
              </ident>
            </identities>
      </member>
</root>')

SELECT  t.c.value('.', 'varchar(20)')
FROM    @YourTable
        CROSS APPLY XmlFile.nodes('root/member/identities/ident/identification[../../../membernum = "0000001" or ../../../membernum = "0000002"]') T (C)

Output:
0000001
George Forman
Confusion
ABC123
Alexander Bell
Dilusion
ABC12345
0
 
LVL 15

Author Closing Comment

by:Ess Kay
ID: 40009502
thanks. i solved this similarly, you are closest to my solution
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

679 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