简体   繁体   中英

XQuery retrieval of a value

I have the following T-SQL that determines if a row exists using two criteria:

Declare @x xml = '
    <row ParentID="45" ObjectID="0" Node="root.local.navigation[7]" itemKey="page" itemValue="Confirmation" itemType="string" />
    <row ParentID="45" ObjectID="0" Node="root.local.navigation[7]" itemKey="visited" itemValue="false" itemType="bool" />'

SELECT @x.exist('/row[@Node eq "root.local.navigation[7]"] and /row[@itemValue eq "Confirmation"]')

Question: Given the above SELECT, how can I SELECT the second row's itemValue? ie Since there's a row with Node="root.local.navigation[7]" and itemValue="Confirmation", return the itemType value in the row where node is the same and itemKey="visited"?

How about this:

declare @x xml = '
<row ParentID="45" ObjectID="0" Node="root.local.navigation[7]" itemKey="page" itemValue="Confirmation" itemType="string" />
<row ParentID="45" ObjectID="0" Node="root.local.navigation[7]" itemKey="visited" itemValue="false" itemType="bool" />'

select case when @x.exist('/row[@Node eq "root.local.navigation[7]"] and /row[@itemValue eq "Confirmation"]') = 1 
    then @x.value('/row[@Node eq "root.local.navigation[7]" and @itemKey eq "visited"][1]/@itemType', 'varchar(50)') 
end as item_type

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