Solved

SELECT on XML field type

Posted on 2007-11-27
8
733 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
[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
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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 500 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grid querry results 41 86
How to simplify my SQL statement? 14 61
convert null in sql server 12 67
SQL works but want to get the XML node data separately 11 57
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 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 antispam), the admini…

752 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