Skip to content

Commit b6c86e7

Browse files
authored
Update dml-merge.md
1 parent 2a93970 commit b6c86e7

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

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

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import FunctionDescription from '@site/src/components/FunctionDescription';
66

77
<FunctionDescription description="Introduced or updated: v1.2.241"/>
88

9-
Performs INSERT, UPDATE, or DELETE operations on rows within a target table, all in accordance with conditions and matching criteria specified within the statement, using data from a specified source.
9+
Performs **INSERT**, **UPDATE**, or **DELETE** operations on rows within a target table, all in accordance with conditions and matching criteria specified within the statement, using data from a specified source.
1010

1111
The data source, which can be a subquery, is linked to the target data via a JOIN expression. This expression assesses whether each row in the source can find a match in the target table and then determines which type of clause (MATCHED or NOT MATCHED) it should move to in the next execution step.
1212

1313
![Alt text](/img/sql/merge-into-single-clause.jpeg)
1414

15-
A MERGE statement usually contains a MATCHED and / or a NOT MATCHED clause, instructing Databend on how to handle matched and unmatched scenarios. For a MATCHED clause, you have the option to choose between performing an UPDATE or DELETE operation on the target table. Conversely, in the case of a NOT MATCHED clause, the available choice is INSERT.
15+
A MERGE statement usually contains a MATCHED and / or a NOT MATCHED clause, instructing Databend on how to handle matched and unmatched scenarios. For a MATCHED clause, you have the option to choose between performing an **UPDATE** or **DELETE** operation on the target table. Conversely, in the case of a NOT MATCHED clause, the available choice is **INSERT**.
1616

1717
## Multiple MATCHED & NOT MATCHED Clauses
1818

@@ -30,17 +30,22 @@ MERGE INTO <target_table>
3030

3131
matchedClause ::=
3232
WHEN MATCHED [ AND <condition> ] THEN
33-
{ UPDATE SET <col_name> = <expr> [ , <col_name2> = <expr2> ... ] | UPDATE * | DELETE }
33+
{
34+
UPDATE SET <col_name> = <expr> [ , <col_name2> = <expr2> ... ] |
35+
UPDATE * |
36+
DELETE /* Removes matched rows from the target table */
37+
}
3438

3539
notMatchedClause ::=
3640
WHEN NOT MATCHED [ AND <condition> ] THEN
3741
{ INSERT ( <col_name> [ , <col_name2> ... ] ) VALUES ( <expr> [ , ... ] ) | INSERT * }
3842
```
3943

40-
| Parameter | Description |
44+
| Parameter | Description |
4145
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
4246
| UPDATE \* | Updates all columns of the matched row in the target table with values from the corresponding row in the source. This requires the column names between the source and target are consistent (though their order can be different) because during the update process, matching is done based on column names. |
4347
| INSERT \* | Inserts a new row into the target table with values from the source row. |
48+
| DELETE | Removes the matched row from the target table. This is a powerful operation that can be used for data cleanup, removing obsolete records, or implementing conditional deletion logic based on source data. |
4449

4550
## Output
4651

@@ -183,3 +188,52 @@ SELECT * FROM target_table order by ID;
183188
4 │ Frank │ 32 │ Edmonton │
184189
└─────────────────────────────────────────────────────────────────────────┘
185190
```
191+
192+
### Example 3: Merge with DELETE Operation
193+
194+
This example demonstrates how to use MERGE to delete records from the target table based on specific conditions from the source table.
195+
196+
```sql
197+
-- Create the customers table (target)
198+
CREATE TABLE customers (
199+
customer_id INT,
200+
customer_name VARCHAR(50),
201+
status VARCHAR(20),
202+
last_purchase_date DATE
203+
);
204+
205+
-- Insert initial customer data
206+
INSERT INTO customers VALUES
207+
(101, 'John Smith', 'Active', '2023-01-15'),
208+
(102, 'Emma Johnson', 'Active', '2023-02-20'),
209+
(103, 'Michael Brown', 'Inactive', '2022-11-05'),
210+
(104, 'Sarah Wilson', 'Active', '2023-03-10'),
211+
(105, 'David Lee', 'Inactive', '2022-09-30');
212+
213+
-- Create the removals table (source with customers to be removed)
214+
CREATE TABLE removals (
215+
customer_id INT,
216+
removal_reason VARCHAR(50),
217+
removal_date DATE
218+
);
219+
220+
-- Insert data for customers to be removed
221+
INSERT INTO removals VALUES
222+
(103, 'Account Closed', '2023-04-01'),
223+
(105, 'Customer Request', '2023-04-05');
224+
225+
-- Enable MERGE INTO
226+
227+
-- Use MERGE to delete inactive customers that appear in the removals table
228+
MERGE INTO customers AS c
229+
USING removals AS r
230+
ON c.customer_id = r.customer_id
231+
WHEN MATCHED AND c.status = 'Inactive' THEN
232+
DELETE;
233+
234+
┌────────────────────────┐
235+
number of rows deleted │
236+
├────────────────────────┤
237+
2
238+
└────────────────────────┘
239+
```

0 commit comments

Comments
 (0)