Concatenate Rows with for xml logic understanding

  • Hi,

    i am not able to understand why just appending blank string in below code removes '<item>' from xml result.Is it converting to varchar datatype ?

    this question is just for my understanding

    CREATE TABLE #tbl

    (id INT IDENTITY(1,1),

    item varchar(100))

    INSERT #tbl

    SELECT 'This'

    UNION ALL

    SELECT'is'

    UNION ALL

    SELECT 'test'

    UNION ALL

    SELECT 'string'

    SELECT item FROM #tbl FOR XML PATH('')

    SELECT ' '+ item FROM #tbl FOR XML PATH('')

    Thanks..

  • You need to provide a name for the column

    SELECT ' '+ item AS item FROM #tbl FOR XML PATH('')

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537
  • In normal result set column value won't affected with column name provided, is this specific to xml?

    I just want to understand logic behind it ,is it converting datatype ?

  • This is specific to XML, it's using the column name as part of the results. There's no data type conversion going on.

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537
  • ok..Thank you Mark

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply