Solved

XML in SQL - Query

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

738 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