1

I have seen some examples querying using a single attribute but am puzzled on how to use 2 attributes.

<componentcache>
  <component Name="CALC_TODAYDATELONG" value="MONDAY, MAY 06, 2013" />
  <component Name="CALC_OFFICENAME" value="DEFAULT OFFICE" />
  <component Name="STAFFINFO_FULLNAME" value="LEE LEE, JR" />
  <component Name="PATINFO_FULLNAME" value="JAYNE H DOE" />
  <component Name="PATINFO_BIRTHDATE" value="11/07/1901" />
  <component Name="PATINFO_PATIENTNO" value="AG000003" />  
  <component Name="ENCOUNT_DXDESC1" value="ABC" />
  <component Name="ENCOUNT_DXDESC2" value="DEF" />
  <component Name="ENCOUNT_DXDESC3" value="HIJK" />
</componentcache>

SELECT DocumentStoreID, DocTemplateID, PersonID, Document from DocumentStore
WHERE DataCache.value('/componentcache/component...

I want to select the row(s) where Name like "ENCOUNT_DXDESC%" and value = 'DEF'

2 Answers 2

2

Try something like this using a CTE (Common Table Expression):

;WITH XmlDataValues AS
(
    SELECT 
        DocumentStoreID,
        CompName = XComp.value('@Name', 'varchar(50)'),
        CompValue = XComp.value('@value', 'varchar(50)')
    from 
        DocumentStore
    CROSS APPLY
        DataCache.nodes('/componentcache/component') XTbl(XComp)  
)   
SELECT * 
FROM XmlDataValues
WHERE CompName LIKE 'ENCOUNT%'
AND CompValue = 'DEF'

The CTE basically takes every row in DocumentStore and gets a list of all <component> XML nodes (as XML) and extracts Name and value attributes from those XML nodes. The CTE then surfaces this information like a relational table - with column names CompName and CompValue. You can easily select from the CTE and use normal T-SQL statements and conditions for that

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

1 Comment

I like this answer as well but it is a bit much for what I want to do but may be of use in the future!
1

Here's a method using XQuery:

SELECT DocumentStoreID, DocTemplateID, PersonID, Document from DocumentStore
WHERE DataCache.exist('/componentcache/component[contains(@Name, "ENCOUNT_DXDESC")][@value="DEF"]')=1

This will return all rows in your table which contain an XML document with a node that has a Name attribute containing "ENCOUNT_DXDESC" and a value attribute of "DEF"

2 Comments

Anyway to have a contains or like here "[@value="DEF"]"?
yep; just replace the [@value="Def"] with [contains(@value, "DEF")]

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.