我有一个返回一些旧的MemberShipIDs
select cst.MembershipID from Contestant cst
inner join User usr on usr.ID = cst.ID and usr.TypeOfUser = 0
order by usr.CreatedOn
我还有另一个查询会返回一些新更新的MemberShipIDs
select
'CON' + '-' + convert(nvarchar(255),
ROW_NUMBER() over(order by CreatedOn)) + '-'
+ Right(Year(usr.CreatedOn),2)
from Contestant cst
inner join [dbo].[User] usr on usr.ID = cst.ID and usr.TypeOfUser = 0
现在,我希望使用新的MemberShipID
值更新旧的MemberShipID
值
对于每个匹配
答案 0 :(得分:0)
您可以在join
中使用update
。这允许您计算子查询或CTE中的新值,并在update
中分配它:
update cst
set MembershipId = newval.newMembershipId
from Contestant cst join
(select cst.MembershipID,
'CON' + '-' + convert(nvarchar(255), ROW_NUMBER() over(order by CreatedOn)) + '-' + Right(Year(usr.CreatedOn),2) as newMembershipId
from Contestant cst inner join
[dbo].[User] usr
on usr.ID = cst.ID and usr.TypeOfUser = 0
) newval
on cst.MembershipID = newval.MembershipID;