如何使用不同的ID更新相同字段的值?
Customer ORderType
Matt 1
Jake 2
对于所有“马特”我想要从杰克将orderType设置为2。
这不起作用。
declare @X nvarchar (250)
set @x = (select OrderType from table where Customer = 'Matt')
update table
set OrderType = @x
where Customer = 'Jake'
答案 0 :(得分:2)
您可以在UPDATE查询中包含FROM子句。
UPDATE C SET OrderType = J.OrderType
FROM [Table] C
CROSS JOIN [Table] J
WHERE C.Customer = 'Matt' AND J.Customer = 'Jake'
如果使用子查询,则该子查询必须恰好返回一行。
另请注意您的引用行:
set @x = (select OrderType from table where Customer = 'Matt')
也可以写成:
SELECT @x = OrderType FROM Table WHERE Customer = 'Matt'
人们倾向于过度使用子查询。
答案 1 :(得分:0)
update table
set OrderType = (select TOP 1 OrderType from table where Customer = 'Matt')
where Customer = 'Jake'
答案 2 :(得分:0)
update table
set OrderType = (select top 1 OrderType from table where Customer = 'Matt')
where Customer = 'Jake'