all groups > sql server programming > june 2006 >
You're in the

sql server programming

group:

SQL 2005 - Select XML Data Field


SQL 2005 - Select XML Data Field Bryan
6/7/2006 8:29:02 PM
sql server programming: Does anyone know how you can select certain XML elements from an XML data
field?

I have an XML field in a table that contains XML like this:
<Form>
<Row/>
<Row/>
</Form>

Right now, I have a query that will find all rows in the table that contains
the XML that I'm looking for, but I want to select particular nodes instead
of the entire XML document.

SELECT XMLDATA
FROM dbo.FormData
WHERE XMLData.exist('(//Form/Row/@MyAttribute="MyValue")')=1

With this I get multiple XML Documents. Is there a way that I can select
RE: SQL 2005 - Select XML Data Field Bryan
6/7/2006 8:54:01 PM
Okay, I found a similar post and I can use:
SELECT XMLDATA.query('/Form/Row')
FROM dbo.FormData
WHERE XMLData.exist('(//Form/Row/@MyAttribute="MyValue")')=1
FOR XML PATH(''), ROOT('Form')

Now, does anyone know how to filter the XML elements that are being appended?
<Form>
<Row MyAttribute="MyValue"/>
<Row MyAttribute="NOTMyValue"/> <!--DELETE-->
</Form>
RE: SQL 2005 - Select XML Data Field Bryan
6/7/2006 9:41:01 PM
Found my problem - I used //Form/Row/@MyAttribute="MyValue" instead of
//Form/Row[@MyAttribute="MyValue"] in the query... Hope this helps other
people... XML in SQL 2005 is great!

SELECT XMLDATA.query('//Form/Row[@MyAttribute="MyValue" and
@MyOtherAttribute="155,100"]')
FROM dbo.FormData
WHERE XMLData.exist('(//Form/Row/@MyAttribute="MyValue")')=1
AddThis Social Bookmark Button