1

whats the SQL for selecting the values from this XML chunk like done in the sample below?

<RWFCriteria reportType="OPRAProject">
   <item id="88" name="" value="" type="Project" />
   <item id="112" name="" value="12" type="Milestone" />
   <item id="43" name="" value="11" type="Milestone" />
</RWFCriteria>

i want to select out similar to this but with the above XML data

DECLARE @Param XML  
SET @Param = '<data>  
      <release id="1"><milestone id="1" /><milestone id="2" /></release>  
      <release id="3"><milestone id="1" /><milestone id="27"/></release>  
    </data>'  

SELECT c.value('../@id', 'INT') AS ReleaseId, c.value('@id', 'INT') AS MilestoneId  
 FROM @Param.nodes('/data/release/milestone') AS T(c)

I want only the data in the nodes where type="Milestone"

1 Answer 1

3

Something like this:

DECLARE @Param XML  
SET @Param = '<RWFCriteria reportType="OPRAProject">
                 <item id="88" name="" value="" type="Project" />
                 <item id="112" name="" value="12" type="Milestone" />
                 <item id="43" name="" value="11" type="Milestone" />
               </RWFCriteria>'  


SELECT 
     RWF.item.value('@id', 'INT') AS 'Id', 
     RWF.item.value('@name', 'VARCHAR(100)') AS 'Name',
     RWF.item.value('@value', 'INT') AS 'Value', 
     RWF.item.value('@type', 'VARCHAR(100)') AS 'Type'
FROM 
     @Param.nodes('/RWFCriteria/item') AS RWF(item)
WHERE
     RWF.item.value('@type', 'VARCHAR(100)') = 'Milestone'

Resulting output:

Id  Name    Value   Type
112           12    Milestone
 43           11    Milestone
Sign up to request clarification or add additional context in comments.

3 Comments

is there a way i can select only the type="milestone" nodes?
Yes, add to the bottom: WHERE RWF.item.value('@type', 'VARCHAR(100)') = 'Milestone'
@kacalapy: sure - updated my answer to select only of type=milestone.

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.