Solved

XML in SQL - Query

Posted on 2014-04-10
5
337 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now