gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:52:51 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com