Pull specific xml node for each row

dunkannuany

New Member
I need to pull a different \[code\]<node>\[/code\] for each row in the table, the position of the node is stored in \[code\]SpecificNode\[/code\]\[code\]DECLARE @table TABLE ( XmlValue XML, SpecificNode INT )INSERT INTO @table SELECT '<root><node>Y</node><node>Y</node><node>10</node><node>YARD</node></root>', 3INSERT INTO @table SELECT '<root><node>N</node><node>20</node><node>PART</node><node></node><node>PASS</node></root>', 2INSERT INTO @table SELECT '<root><node>Y</node><node>30</node><node>FORK</node></root>', 2\[/code\]I can pull a specified node, but when I try to make it dynamic it give me the error "The argument 1 of the xml data type method "value" must be a string literal."\[code\]SELECT XmlValue.value('(/root/node)['+SpecificNode+']', 'VARCHAR(100)')FROM @table AS tbl\[/code\]Same error with this\[code\]SELECT x.value, XmlValue.value(x.value, 'VARCHAR(100)')FROM @table AS tblCROSS APPLY (SELECT '(/root/node)['+CONVERT(VARCHAR, SpecificNode)+']' as value) X\[/code\]My expected output would be\[code\]102030\[/code\]
 
Back
Top