Skip to content

Commit 40b6cfb

Browse files
authored
Update dml-update.md (#1712)
1 parent cc2ecc3 commit 40b6cfb

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

docs/en/sql-reference/10-sql-commands/10-dml/dml-update.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: UPDATE
33
---
44
import FunctionDescription from '@site/src/components/FunctionDescription';
55

6-
<FunctionDescription description="Introduced or updated: v1.2.699"/>
6+
<FunctionDescription description="Introduced or updated: v1.2.705"/>
77

88
Updates rows in a table with new values, optionally using values from other tables.
99

@@ -20,6 +20,88 @@ UPDATE <target_table>
2020
[ WHERE <condition> ] -- Filter rows
2121
```
2222

23+
## Configuring `error_on_nondeterministic_update` Setting
24+
25+
The `error_on_nondeterministic_update` setting controls whether an error is returned when an UPDATE statement attempts to update a target row that joins multiple source rows without a deterministic update rule.
26+
27+
- When `error_on_nondeterministic_update` = `true` (default): Databend returns an error if a target row matches multiple source rows and there is no clear rule for selecting which value to use.
28+
- When `error_on_nondeterministic_update` = `false`: The UPDATE statement proceeds even if a target row joins multiple source rows, but the final update result may be non-deterministic.
29+
30+
Example:
31+
32+
Consider the following tables:
33+
34+
```sql
35+
CREATE OR REPLACE TABLE target (
36+
id INT,
37+
price DECIMAL(10, 2)
38+
);
39+
40+
INSERT INTO target VALUES
41+
(1, 299.99),
42+
(2, 399.99);
43+
44+
CREATE OR REPLACE TABLE source (
45+
id INT,
46+
price DECIMAL(10, 2)
47+
);
48+
49+
INSERT INTO source VALUES
50+
(1, 279.99),
51+
(2, 399.99),
52+
(2, 349.99); -- Duplicate id in source
53+
```
54+
55+
Executing the following UPDATE statement:
56+
57+
```sql
58+
UPDATE target
59+
SET target.price = source.price
60+
FROM source
61+
WHERE target.id = source.id;
62+
```
63+
64+
- With `error_on_nondeterministic_update = true`, this query fails because id = 2 in target matches multiple rows in source, making the update ambiguous.
65+
66+
```sql
67+
SET error_on_nondeterministic_update = 1;
68+
69+
root@localhost:8000/default> UPDATE target
70+
SET target.price = source.price
71+
FROM source
72+
WHERE target.id = source.id;
73+
74+
error: APIError: QueryFailed: [4001]multi rows from source match one and the same row in the target_table multi times
75+
```
76+
77+
- With `error_on_nondeterministic_update = false`, the update succeeds, but target.price for id = 2 may be updated to either 399.99 or 349.99, depending on execution order.
78+
79+
```sql
80+
SET error_on_nondeterministic_update = 0;
81+
82+
root@localhost:8000/default> UPDATE target
83+
SET target.price = source.price
84+
FROM source
85+
WHERE target.id = source.id;
86+
87+
┌────────────────────────┐
88+
number of rows updated │
89+
├────────────────────────┤
90+
2
91+
└────────────────────────┘
92+
93+
SELECT * FROM target;
94+
95+
┌────────────────────────────────────────────┐
96+
│ id │ price │
97+
├─────────────────┼──────────────────────────┤
98+
1279.99
99+
2399.99
100+
└────────────────────────────────────────────┘
101+
```
102+
103+
104+
23105
## Examples
24106

25107
The following example demonstrates how to update rows in a table, both directly and using values from another table.

0 commit comments

Comments
 (0)