简体   繁体   中英

Using XQuery in SQL Server

My table contains 2 columns, one is the Id of type int and the other is of type Xml .

In the Xml column, I have data of this structure

<Fields>
    <Field name='aa' value='000'/>
    <Field name='bb' value='111'/>
    <Field name='cc' value='222'/>
</Fields>

Now using XQuery, I want to query the table to get all rows where field tag with name attribute is aa and value attribute is 111 , AND field tag with name is attribute bb and value attribute is 222 .

In other words I want to supply the name / value pair and get rows where the xml data match.

Thank you !

You could use something like this (since you didn't mention your table or column names, I just used something on my own - adapt those as needed!):

SELECT 
    (list of columns)
FROM 
    dbo.YourTableNameHere
WHERE 
    XmlContent.exist('/Fields/Field[@name = "aa"][@value = "000"') = 1

This would return all rows from your table where the XML contains a <Field> node with attributes name = "aa" and value = "000"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM