Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XML in SQL - Query

Posted on 2014-04-10
5
Medium Priority
?
422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

610 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