SQL Server update语句中使用where column in(重复值)-需要更新多少个记录?

3

当我在SQL Server中执行此操作时

update table 
set column = @value
where id in (1,2,2)

需要执行多少次更新操作?2 次还是 3 次,其中两次更新作用于 id=2 的行?


为什么不直接测试一下呢? - Eiko
好的,我明白了,这很简单... - Przemysław Niedziela
你可以将测试代码和结果发布为答案,并稍后接受它作为自己和他人的参考。 :) - Eiko
如果id是主键并且唯一的主键,那么就是2..否则谁知道呢? - lordkain
2个回答

2

使用select测试你的代码,你将看到有多少行将被更新。

计数

select count(*) from table
where id in (1,2,2)

查看更新的行

select * from table
where id in (1,2,2)

如果id是主键(并且是唯一的,只有2行)

1
declare @table table (id int, num int)
insert into @table values (1,1),(2,1)

update @table
set num = num + 1
where id in (1,2,2)

select * from @table

Result is:

id  num
1   2
2   2

因此,更新将在集合中的每个唯一值上执行一次。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接