4

I am using SQL Server 2008

I have a table with xml data looks like:

ID           xml_col  
1            <Attribute id="7">1.0</Attribute><Attribute id="8">AAA</Attribute>  
2            <Attribute id="7">2.0</Attribute><Attribute id="8">BBB</Attribute>  
3            <Attribute id="7">3.0</Attribute><Attribute id="8">AAA</Attribute>  
4            <Attribute id="7">1.0</Attribute><Attribute id="8">BBB</Attribute>  
5            <Attribute id="7">1.0</Attribute><Attribute id="8">AAA</Attribute>  
6            <Attribute id="7">7.0</Attribute><Attribute id="8">CCC</Attribute>  

I want to get those records that

'when attribute=17 then value =1'+ 'when attribute=8 then value='AAA''

How can i create the query and how to build xml indexes?

Thank you very much.

1
  • If you want to post code or XML, please highlight those lines and click on the "code" button (101 010) on the editor toolbar. That formats the code/XML nicely, and provides syntax highlighting, and you don't have to mess around with your XML codes (no need to replace the < and > characters)! Do not use <pre> - doesn't work here Commented Jun 7, 2010 at 9:37

1 Answer 1

5

To select all rows which have the criteria you mentioned, try this select statement:

SELECT * 
FROM dbo.YourXmlTable
WHERE
YourXmlTable.xml_col.value('(//Attribute[@id=7])[1]', 'decimal') = 1.0
AND 
    YourXmlTable.xml_col.value('(//Attribute[@id=8])[1]', 'varchar(10)') = 'AAA'

For XML indices, read up on Books Online how to create them and how to use them.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Base on your information, I have created indexes as below: CREATE PRIMARY XML INDEX idx_xCol on TABLEA (XML_col) GO CREATE XML INDEX idx_xCol_Path on TABLEA (XML_col) USING XML INDEX idx_xCol FOR PATH GO CREATE XML INDEX idx_xCol_Value on TABLEA(XML_col) USING XML INDEX idx_xCol FOR VALUE but the performance looks not very good, any recommendation for indexes?
@envykok: if you have an XML index for everything, that's about all you can do. Reaching into XML using XQuery is not known to be an awfully fast operation..... if you need to query certain elements over and over again, extract them into the relational table structure, instead of having them inside the XML column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.