0

我必须使用通过 xmlpath 获得的连接字符串的结果来更新表列。

例如,我必须做这样的事情

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 (''))

我收到错误“c 附近的语法不正确”。

PS:在我的示例中,字段 Languages 的类型为 nvarchar(max)

有人可以帮助我吗?谢谢

4

2 回答 2

0

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
于 2014-02-05T11:10:24.343 回答
0

这样写可能对你有帮助

这是您必须尝试这样的演示代码:

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'))
于 2014-01-31T09:17:49.173 回答