SQL多次更新同一行(SQL update same row multiple times)

编程入门 行业动态 更新时间:2024-10-28 10:35:45
SQL多次更新同一行(SQL update same row multiple times)

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

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倍。 这一行只访问过一次吗? 如何计算最终值?

One example where we could consider the necessity of multiple updates to the same row:

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

There are 3 potential updates to the same row. The join result set has 3 times the same row. Is this row visited only once? How is the final value computed?

最满意答案

从UPDATE 文档 :

指定FROM子句时要小心,以提供更新操作的条件。 如果语句包含未以这样的方式指定的FROM子句,那么UPDATE语句的结果是未定义的,即只有一个值可用于更新的每个列事件,即UPDATE语句不是确定性的。

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

From the UPDATE 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.

The results are non deterministic in this cases. Better to consider those SQL statements as errors.

更多推荐

本文发布于:2023-07-22 06:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1217750.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:update   SQL   row   times   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!