[英]Concatenate values from a table then update a field in a different table with those values
我需要根据表 1 中的 AuthorID 用来自表 2 的串联数据填充表 1 中的一个字段。表 1 中的 ID 是一个由 ; 分隔的字符串。 例如'234;242;54'
我找到了一个将 CSV 转换为表格的函数,然后我可以使用这些值来获取与这些 ID 关联的 GUID 并将它们连接起来
SELECT TOP 100 *,
stuff((
select ';' + convert(varchar(200),AuthorGuid)
from
(select t.AuthorGuid
from Table2 t
where t.AuthorID in (select * from dbo.CSVToTable(REPLACE(w.Authors,';',',')))
) auths
for xml path('')), 1, 1, '') as AuthorGUIDStemp
FROM Table1 w
上面的查询将 GUIDS 放在 Table1 的临时字段中,但我需要更新 Table1
update Table1 w set AuthorGUIDs =
stuff((
select ';' + convert(varchar(200),AuthorGuid)
from
(select t.AuthorGuid
from Table2 t
where t.AuthorID in (select * from dbo.CSVToTable(REPLACE(Authors,';',',')))
) auths
for xml path('')), 1, 1, '')
这不适用于更新字段并引发错误。
消息 102,级别 15,状态 1,第 15 行
'w' 附近的语法不正确。
消息 156,级别 15,状态 1,第 23 行
关键字“for”附近的语法不正确。
我如何使此更新工作?
看起来您的更新语法可能已关闭。 我相信您正在尝试使用 XML 类型更新 AuthorGuids。
假设你想要的结果是文本,试试这个:
UPDATE w
SET AuthorGUIDs = STUFF(( SELECT ';' + CONVERT(VARCHAR(200), AuthorGuid)
FROM
( SELECT t.AuthorGuid
FROM Table2 t
WHERE t.AuthorID IN(SELECT * FROM dbo.CSVToTable(REPLACE(Authors, ';', ',')) ))auths
FOR XML PATH(''), TYPE).value('.', 'varchar(max'), 1, 1, '')
FROM Table1 w
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.