Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SELECT on XML field type

Posted on 2007-11-27
8
Medium Priority
?
750 Views
Last Modified: 2012-06-27
I have a database with a column of type "xml".  I have data such as this in the column:

<Customer>ABC Company</Customer>
<Customer>XYZ Company</Customer>
<OrderNo>123</OrderNo>
<Customer>ABC Company</Customer>

Can I run a SELECT statement to find all records that have a Customer of "ABC Company" in this column?  In other words, the first and fourth records only?

How would I do this?

Thanks,
Chris

0
Comment
Question by:chlade
  • 3
  • 3
  • 2
8 Comments
 
LVL 7

Expert Comment

by:Chandan_Gowda
ID: 20363490
Please follow the below link i think it might help you
http://msdn2.microsoft.com/en-us/library/ms189075.aspx
0
 

Author Comment

by:chlade
ID: 20364859
Thanks.  I can't seem to quite get it.  Here is my SELECT statement:

SELECT keyInfo.query('declare namespace AWMI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";        
    /AWMI:root[@Customer="ABC Company"]
') AS Result
FROM tblSQLEventLog

My data in the KeyInfo column looks like the examples in my first post.

What am I missing?
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20364955
SELECT *
FROM tblSQLEventLog
WHERE keyInfo.query('declare namespace AWMI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";        
    /AWMI:root[@Customer="ABC Company"]
') Is not null
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:Chandan_Gowda
ID: 20364962
can u share the error message
0
 

Author Comment

by:chlade
ID: 20365132
My original select statement was returning all rows but the columns showed up empty (not sure if it's null, spaces, or empty - to me it looks empty)

imitchie, I tried that but it returned all rows with the actual value in the column
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20367949
i think the xpath is incorrect, but test null is wrong. i'll test some more
0
 
LVL 25

Accepted Solution

by:
imitchie earned 2000 total points
ID: 20368816
SELECT *
FROM tblSQLEventLog
WHERE keyInfo.exist('declare namespace AWMI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";        
    /Customer[/="ABC Company"]
') = 1

I can't get the namespace to work, probably because you have not shown the full XML. however the key points are:
1. use xml.exist() = 1 to test
2. /Customer[@Customer="X"] is testing for the Attribute Customer in the node Customer, whereas /Customer[/="X"] is testing for the node value of Customer
0
 

Author Comment

by:chlade
ID: 20370963
I just copied the namespace part from another sample.  It looks like it's not necessarily needed.  But the SELECT statement you gave worked great.

Here's the statement I'm now using:
SELECT * FROM tblSQLEventLog
WHERE keyInfo.exist('/Customer[/="ABC Company"]') = 1

Thanks for the help!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

927 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