Question

I have to update a table column with a result of a concatened string obtained with xmlpath.

For exemple, I have to do something like that

Update table c
set c.languages = 
(SELECT '\r\n'+rl.Name AS [text()] 
    from ResourceLanguage as rl 
        where rl.resource_Id=c.resource_Id
    FOR XML PATH (''))

I get an error 'Incorrect syntax near c'.

P.S : In my exemple, the field Languages is of type nvarchar(max)

Can somebody help me? Thanks

Was it helpful?

Solution 2

I found the solution. Here is it:

 DECLARE @tempTable TABLE
(
    Id int,
    Languages nvarchar(max)
)

INSERT @tempTable (Id, Languages )
(
    select r1.resource_Id, Stuff((select', '+ rl.Name as [text()]
      from ResourceLanguage as rl 
      where rl.resource_Id=c.resource_Id
      for xml path(''), type).value('.', 'nvarchar(250)'), 1, 2, '')
    from Resource r1
)

Update Resource 
Set Languages = temp.Languages 
    FROM @tempTable temp  
    where resource_Id=temp.resource_Id

OTHER TIPS

Write This Way MAy Be Help You

This a Demo Code For That You Have TO Try Like This:

UPDATE    Table_1

SET    c =

(

SELECT ( SELECT 'White' AS Color1,

'Blue' AS Color2,

'Black' AS Color3,

'Light' AS 'Color4/@Special',

'Green' AS Color4,

'Red' AS Color5

FOR

XML PATH('Colors'),

TYPE

),

( SELECT 'Apple' AS Fruits1,

'Pineapple' AS Fruits2,

'Grapes' AS Fruits3,

'Melon' AS Fruits4

FOR

XML PATH('Fruits'),

TYPE

)
FOR XML PATH(''),

ROOT('SampleXML'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top