In a former post about generic creation of pivot table I found a working example. I only fail to fully understand the part where the XML ends up to be a varchar column.
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(product)
from product
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
I tried to look it up in both the description of the STUFF statement and the FOR XML, but not able to find a explanation of the used code [.value('.', 'NVARCHAR(MAX)')]
. It converts XML to VARCHAR
, but in which context?
Also in this post there is a partial reference:
The .value('.', 'varchar(max)') simply retrieves the value from the resulting XML fragment, without XML-encoding any "special" characters.
It does not makes clear to me in which context the additional .value is coming from. Is it part of the SELECT or the XML or the STUFF? Can someone explain the context of it? Or point me to the correct documentation?