I have the following sample xml in a column of xml data type:
<diagnostics CurrentIterationNumber="79" TotalItemsProcessed="37847"
ItemsProcessedLastIteration="75" ItemsProcessedPerIteration="479" />
I want to have a stored proc where I am able to pass in the name of the attribute to query. I then want to store its value into a variable to perform another set of calculations.
I have read that the .value function expects a string literal and many resources on the web advise to use the sql:variable() function for this. However, I am left with the code returning the actual contents of the variable.
Trying something along the following lines:
declare @property as varchar(50);
set @property = '@CurrentIterationNumber';
declare @x as xml;
set @x = (select PropertyValues from dbo.tblDiagnosticsSnapshot);
SELECT
t.c.value('sql:variable("@property")', 'varchar(50)')
FROM
@x.nodes('/diagnostics') t(c);