In my SQL Server table one column has datatype as XML. It contains data as:
<P1>
<P2>
<P3 name='[1] name1', value='val1'> </P3>
<P3 name='[2] name2', value='val2'> </P3>
<P3 name='[3] name3', value='val3'> </P3>
</P2>
</p1>
I am fetching name1, name2, name3 using query:
select top(1)
col.value('(/P1[1]/P2[1]/P3/@name)[1]', 'VARCHAR(max)') as q1,
col.value('(/P1[1]/P2[1]/P3/@name)[2]', 'VARCHAR(max)') as q2,
col.value('(/P1[1]/P2[1]/P3/@name)[3]', 'VARCHAR(max)') as q3,
FROM table
How can I loop on this data to get all the names. Something like:
declare @x=1
while @x<4:
begin
select top(1)
col.value('(/P1[1]/P2[1]/P3/@name)[@x]', 'VARCHAR(max)') as q@x
from table
set @x=@x+1
end
Also how to loop on the same XML to get all the values?