简体   繁体   中英

tsql for xml string concat - How it works

I have the following code which works well:

    STUFF(  ( 
                        select 
                            char(13)+'Item '+i.item+' : '--+char(13) +i.item_descr
                        from @itemlines i
                        where i.customer=main.customer
                        FOR XML PATH(''), TYPE
                    ).value('.','varchar(max)')
                ,1,1, '')

What is the .value() thing? Something like a...select method? What does it do? Any reference links will be appreciated too!

FOR XML will return an XML datatype; the .value(...,...) pulls out the XML value and converts it to the datatype defined. In your case, everything in the root node ('.') converted to varchar(max)

For some blogs/links look at Aaron Bertrand's post or Adam Machanic's also watch out for STRING_AGG a new function in SQL2017

You use FOR XML PATH to convert your table into XML. XML data are represented by a XML data type in SQL Server and it is possible to process a list of different methods on this data type. One of these methods is a value method which has two arguments: XQuery and Data type. The method allows you to convert the data in XML into some other format (varchar in your case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM