I need to pull a different <node> for each row in the table, the position of the node is stored in SpecificNode
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>', 3
INSERT INTO @table SELECT '<root><node>N</node><node>20</node><node>PART</node><node></node><node>PASS</node></root>', 2
INSERT INTO @table SELECT '<root><node>Y</node><node>30</node><node>FORK</node></root>', 2
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.”
SELECT
XmlValue.value('(/root/node)['+SpecificNode+']', 'VARCHAR(100)')
FROM @table AS tbl
Same error with this
SELECT
x.value,
XmlValue.value(x.value, 'VARCHAR(100)')
FROM @table AS tbl
CROSS APPLY (SELECT '(/root/node)['+CONVERT(VARCHAR, SpecificNode)+']' as value) X
My expected output would be
10
20
30
You can use sql:column