Solved

SQL 2005 Xpath query help needed

Posted on 2009-07-08
2
458 Views
Last Modified: 2013-11-11
Hi experts,

I need some help with making a query with a SQL 2005 XML column.

I have a table called EventLog, and this table has a column Details that is of type XML and contains details about an event log entry. Different event log entries may have different details. The XML for some of the entries looks like this:

(simplified)

<EventDetails>
  <item>
    <key>
      <string>UserID</string>
    </key>
    <value>
      <string>1</string>
    </value>
  </item>
  <item>
    <key>
      <string>InvoiceID</string>
    </key>
    <value>
      <string>13</string>
    </value>
  </item>
</EventDetails>

This one has two elements, UserID and InvoiceID, and their respective values.

What I need to do is to write a SQL query that will return * FROM EventLog WHERE the Details column has a InvoiceID element and its value is N (e.g. 123). This way I can find all event log entries that are somehow related to invoice 123.

Thanks in advance!
0
Comment
Question by:kerzner
2 Comments
 
LVL 1

Author Comment

by:kerzner
ID: 24857981
OK, nobody here can write that xpath query!?
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 24875253
SELECT      *
FROM      EventLog
WHERE      Details.exist('/EventDetails/item/key[string="InvoiceID"]') = 1
      AND Details.exist('/EventDetails/item[value="13"]') = 1
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 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