Skip to content
Advertisement

Get all values for the XML file in SQL

I have tried the @Name and I get all the values such as DefCurrency, IsICP etc but I don’t get the values.

See my code below.

Thanks in advance

Advertisement

Answer

All supported versions of Microsoft SQL Server have the nodes() and value() methods available for querying the XML data type, e.g.:

Which yields the output:

LABEL val
DefCurrency [None]
AllowAdjs N
IsICP N
AllowAdjFromChildren N
SecurityClass NONE
UserDefined1
UserDefined2
UserDefined3
HoldingCompany
SecurityAsPartner

As mentioned by @Charlieface you can also query your table directly and cross apply to use the nodes() method:

It is possible to stack multiple layers of cross apply ... .nodes(), so to include the content of the LABEL element you can modify the code like the following:

Which yields the output:

LabelValue LABEL val
[None] DefCurrency [None]
[None] AllowAdjs N
[None] IsICP N
[None] AllowAdjFromChildren N
[None] SecurityClass NONE
[None] UserDefined1
[None] UserDefined2
[None] UserDefined3
[None] HoldingCompany
[None] SecurityAsPartner

In all likelihood, though, you’re not wanting to repeat the LABEL element’s value across multiple rows so you can instead use XQuery to filter on the Name attributes and effectively pivot the data with the following:

LabelValue DefCurrency AllowAdjs IsICP AllowAdjFromChildren SecurityClass UserDefined1 UserDefined2 UserDefined3 HoldingCompany SecurityAsPartner
[None] [None] N N N NONE
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement