sql-server - SQL多次更新同一行

标签 sql-server sql-update

一个例子,我们可以考虑对同一行进行多次更新的必要性:

create table t ( c1 int );
insert into t (c1) values (1);
insert into t (c1) values (2);
insert into t (c1) values (3);

update x1
set x1.c1 = X2.new
from t as x1
inner join
(
    select 1 as c1, 100 as new 
    union sel 1, 101
    union sel 1, 102
) as x2 on x1.c1 = x2.c1

同一行可能有 3 次更新。连接结果集有 3 次相同的行。这一行只访问过一次吗?最终值是如何计算的?

最佳答案

来自更新documentation :

Use caution when specifying the FROM clause to provide the criteria for the update operation. The results of an UPDATE statement are undefined if the statement includes a FROM clause that is not specified in such a way that only one value is available for each column occurrence that is updated, that is if the UPDATE statement is not deterministic.

在这种情况下,结果是不确定的。最好将这些 SQL 语句视为错误。

关于sql-server - SQL多次更新同一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17280518/

相关文章:

c# - SqlCommandBuilder 内部连接更新

sql-server - 在 SQL Server 数据库中记录表和列的含义的好方法是什么?

sql-server - SQL Server 是否可以在同一个事务的同一行上应用 s 锁和 x 锁?

sql - 我尝试创建接收 3 个参数的存储过程,如果任何参数的值等于 "-1",它将不会包含在 select 语句中

sql - 如何在 SQLite 上使用 CTE 进行更新/删除?

mysql - 如何使用 JOIN 更新 SELECT 表中的字段

python - 使用 SqlAlchemy 和 PostgreSQL 更新 .. LIMIT 1

sql-server - 用变量重命名逻辑名称

c# - 将 EF6 数据库优先从 SQL Server 迁移到 PostgreSQL

sql - 根据列的当前值更新列