Solved

SELECT on XML field type

Posted on 2007-11-27
8
706 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
 
LVL 7

Expert Comment

by:Chandan_Gowda
ID: 20364962
can u share the error message
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Stored Procedure error 45 47
SQL Where not exists in same table 3 51
INSERT DATE FROM STRING COLUMN 18 46
create insert script based on records in a table 4 12
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
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…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

19 Experts available now in Live!

Get 1:1 Help Now