Solved

XML in SQL - Query

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

856 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