I am, looking for a query to get all attribute values "NetAmount" of Element "PRODUCT" from the following samle XML:
<ROOT>
<CATEGORY name="A">
<PRODUCT NetAmount="10.50"/>
<PRODUCT NetAmount="20.50"/>
</CATEGORY>
<CATEGORY name="B">
<PRODUCT NetAmount="30.50"/>
<PRODUCT NetAmount="40.50"/>
<PRODUCT NetAmount="75.50"/>
</CATEGORY>
</ROOT>
For my scenario it does not matter wether the XML is stored in a table or variable.
I tried several different approaches but all failed.
Sample:
SELECT
NetAmount.value('(@NetAmount)[1])','varchar(250)') AS Products
FROM
@x.nodes('//ROOT/Category/Product')
AS P(NetAmount)
I usually get the following error message that makes no sense to me:
Meldung 2370, Ebene 16, Status 1, Zeile 5 XQuery [value()]: Am Ende
des XQuery-Ausdrucks wurden keine Token mehr erwartet. Gefunden: ')'.
Best Answer
Almost get it:
db<>fiddle here