I am needing to query for all the id values in the XML column below and needing some help doing that. How do I use the index values in my select statement? Any help/direction would be appreciated. Thanks.
Here is the XML file:
<rotation>
<adjuster id="3381" index="0" />
<adjuster id="7629" index="1" />
<adjuster id="10087" index="2" />
<adjuster id="10741" index="3" />
<adjuster id="11544" index="4" />
<adjuster id="12367" index="5" />
</rotation>
Here is my select statement but only getting the first value returned:
select
t.AssignmentRotation.value('(/rotation/adjuster/@id)[1]','varchar(max)') as adjuster_id
from
dbo.CMS_AdjusterTeam t
where
t.AdjusterTeamSysID IN (5, 6);
Following snippet illustrate how to extract all Id and index values:
declare @xml xml = N'<rotation>
<adjuster id="3381" index="0" />
<adjuster id="7629" index="1" />
<adjuster id="10087" index="2" />
<adjuster id="10741" index="3" />
<adjuster id="11544" index="4" />
<adjuster id="12367" index="5" />
</rotation>'
select Id = rt.aj.value('@id', 'varchar(5000)'),
[Index] = rt.aj.value('@index', 'varchar(5000)')
from (select XmlData = @xml) t
cross apply t.XmlData.nodes('//rotation/adjuster') rt(aj)
So your final query would be looking like:
select rt.aj.value('@id','varchar(max)') as adjuster_id
from dbo.CMS_AdjusterTeam t
cross apply t.AssignmentRotation.nodes('//rotation/adjuster') rt(aj)
WHERE t.AdjusterTeamSysID IN (5, 6);
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.