Solved

SELECT on XML field type

Posted on 2007-11-27
8
722 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL help 5 56
Query - which index being used? 2 60
CREATE DATABASE ENCRYPTION KEY 1 72
INSERT DATE FROM STRING COLUMN 18 59
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 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