Starting with XML DML in SQL Server, pretty fine at the moment, but I am facing this challenge. I need to iterate through some defined nodes in XML data stored in SQL Server.
Already check this as reference, it gives a clue but still I did not figure it out how to send a SQL variable as an index in XML DML Reference.
Suppose the following XML data:
<materials>
<est_mat>
<pos>20</pos>
<item>BOX</item>
<qty>0.004</qty>
</est_mat>
<est_mat>
<pos>30</pos>
<item>xxx-xxx-xxx01</item>
<qty>1</qty>
</est_mat>
<est_mat>
<pos>40</pos>
<item>xxx-xxx-xxx02</item>
<qty>1</qty>
</est_mat>
</materials>
So what I am looking is to iterate through all number of <est_mat>
nodes and replace <pos>
attribute starting from 10, then next node will be 20 and so on.
So far I have this :
--starting of code
declare @cnt int = 10
declare @totalchildren varchar(300)
declare @pos int = 1
--returns the number of nodes
SET @totalchildren = (SELECT (XMLData.value('count(/materials/est_mat)', 'int'))
FROM TABLE_XMLFiles
WHERE myref = 173)
WHILE @cnt < @totalchildren
BEGIN
--PRINT @cnt
UPDATE TABLE_XMLFiles
SET XMLData.modify('replace value of (/materials/est_mat/pos[sql:variable("@pos")])[[1]] with sql:variable("@cnt")')
WHERE myref = 173
SET @cnt = @cnt + 1
SET @pos = @pos + 10
END
--end of code
Error:
XQuery [BinControl_XMLFiles.XMLData.modify()]: The target of 'replace value of' must be a non-metadata attribute or an element with simple typed content, found 'element(pos,xdt:untyped) ?'
Question is: how I can send a SQL variable as an index position like this:
SET XMLData.modify('replace value of (/materials/est_mat/pos/text())[sql:variable("@pos")]
with sql:variable("@cnt")')
as the value which I am replacing it works by sending it this way with sql:variable("@cnt")
- already tried it and works but I am still not figuring it out how to send a variable through the index context.
Thanks in advance for your attention.