You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 REPLACETABLEtarget (
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 REPLACETABLEsource (
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
+
SETtarget.price=source.price
60
+
FROM source
61
+
WHEREtarget.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
+
SETtarget.price=source.price
71
+
FROM source
72
+
WHEREtarget.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
+
SETtarget.price=source.price
84
+
FROM source
85
+
WHEREtarget.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
+
│ 1 │ 279.99 │
99
+
│ 2 │ 399.99 │
100
+
└────────────────────────────────────────────┘
101
+
```
102
+
103
+
104
+
23
105
## Examples
24
106
25
107
The following example demonstrates how to update rows in a table, both directly and using values from another table.
0 commit comments