This site has a technique to pass xml data around in Microsoft SQL Server:
DECLARE @productIds xml
SET @productIds ='<Products><id>3</id><id>6</id><id>15</id></Products>'
SELECT
ParamValues.ID.value('.','VARCHAR(20)')
FROM @productIds.nodes('/Products/id') as ParamValues(ID)
But what is the syntax if I add another field?
The following does NOT work:
DECLARE @productIds xml
SET @productIds ='<Products><id>3</id><descr>Three</descr><id>6</id><descr>six</descr><id>15</id><descr>Fifteen</descr></Products>'
SELECT
ParamValues.ID.value('.','VARCHAR(20)')
,ParamValues.descr.value('.','VARCHAR(20)')
FROM @productIds.nodes('/Products/id') as ParamValues(ID)
Note: Maybe I’ve constructed my xml wrong.
You need to use something like:
That FROM statement there defines something like a “virtual table” called
ParamValues.ID– you need to select the<Products>node into that virtual table and then access the properties inside it.Furthermore, your XML structure is very badly chosen:
You won’t be able to select the individual pairs of id/descr – you should use something more like:
Then you could retrieve all items using this SQL XML query: