我想更新massemail_emailmaster的GroupID列,并将其设置为massemail_groupmaster上存储的值。两个表都有GroupName列,我用它来加入子查询。 但是以下查询给了我错误。错误消息是a3附近的语法不正确。我不明白这里的语法错误。请帮忙。
UPDATE [dbo].[massmail_emailmaster] a3
set a3.GroupId =
(select TOP 1 a1.GroupID from [dbo].[massmail_groupmaster] a1
join [dbo].[massmail_emailmaster] a2
on a1.[groupname] = a2.[groupname]
where a3.[GroupName]=a2.[GroupName])
表[dbo]. [massmail_emailmaster]
(ClientID, varchar(50)),
(uid, int)
(Name, varchar(100))
(GroupName, varchar(100))
(Email, varchar(100))
GroupId, int)
表格的DDL
[dbo]. [massmail_groupmaster]
(Clientid, varchar(50))
,(uid, int)
,(groupname, varchar(100))
(GroupId,int))
答案 0 :(得分:2)
稍后在update
子句中定义时,可以使用from
中的别名。 (这是一种“反常”的情况,其中别名在首次使用后定义。)
UPDATE a3
set GroupId = (select TOP 1 a1.GroupID
from [dbo].[massmail_groupmaster] a1
where a1.[groupname] = a3.[groupname]
)
FROM [dbo].[massmail_emailmaster] a3;
答案 1 :(得分:1)
UPDATE a2
SET a2.GroupId = (SELECT TOP 1 a.GroupID
FROM [dbo].[massmail_groupmaster] a
INNER JOIN [dbo].[massmail_emailmaster] b
ON a.[groupname] = b.[groupname]
)
FROM [dbo].[massmail_emailmaster] a2
WHERE a.[groupname] = a2.[groupname]