-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3842 from EnterpriseDB/release/2023-03-23
Release: 2023-03-23
- Loading branch information
Showing
15 changed files
with
880 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
product_docs/docs/epas/15/epas_compat_sql/65a_merge.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
--- | ||
title: "MERGE" | ||
--- | ||
|
||
## Name | ||
|
||
`MERGE`Β —Β conditionally insert, update, or delete rows of a table. | ||
|
||
## Synopsis | ||
|
||
```sql | ||
MERGE INTO target_table_name [ target_alias ] | ||
USING data_source [ source_alias ] ON ( join_condition ) | ||
-- merge_update_clause | ||
WHEN MATCHED THEN | ||
UPDATE SET column = value, ... [ WHERE where_condition ] | ||
[ DELETE [ WHERE where_condition ] ] | ||
|
||
-- merge_insert_clause | ||
WHEN NOT MATCHED THEN | ||
INSERT [(col_list)] VALUES (val_list) [ WHERE where_condition ] | ||
``` | ||
|
||
## Description | ||
|
||
`MERGE` allows you to select rows from one or more sources for update or insertion into a table. You specify the join condition to determine whether to update or insert into the target table. You specify conditional UPDATE and INSERT statements using the WHERE clause in the MERGE statement. | ||
|
||
It allows you to combine multiple operations and avoid multiple INSERT, UPDATE, and DELETE DML statements. | ||
|
||
For more information, see [MERGE statement](https://www.postgresql.org/docs/current/sql-merge.html). | ||
|
||
|
||
## Parameters | ||
|
||
`target_table_name` | ||
|
||
The name (optionally schema-qualified) of the target table to merge into. | ||
|
||
`target_alias` | ||
|
||
A substitute name for the target table. When an alias is provided, it completely hides the actual name of the table. | ||
|
||
`data_source` | ||
|
||
The name (optionally schema-qualified) of the source table or a source query. The source table is the source table name, view name, or transition table name. The source query (SELECT or VALUES statement) supplies the rows to be merged into the target_table_name. | ||
|
||
`source_alias` | ||
|
||
A substitute name for the data source. When an alias is provided, it completely hides the actual name of the table. | ||
|
||
`join_condition` | ||
|
||
An expression resulting in a value of type boolean similar to a WHERE clause that specifies which rows in the data_source match rows in the target_table_name. | ||
|
||
`merge_update_clause` | ||
|
||
Specifies the new column values of the target table. It performs this update if the join_condition of the ON clause is true. | ||
|
||
Specify the where_condition if you want the database to execute the update operation only if the specified condition is true. The condition can refer to either the data source or the target table. If the condition is not true, then the database skips the update operation when merging the row into the table. | ||
|
||
Specify the DELETE where_condition to clean up data in a table while populating or updating it. The only rows affected by this clause are those rows in the target table that are updated by the merge operation. The DELETE WHERE condition evaluates the updated value, not the original value that was evaluated by the UPDATE SET ... WHERE condition. If a row of the target table meets the DELETE condition but is not included in the join defined by the ON clause, then it is not deleted. | ||
|
||
You can specify this clause by itself or with the merge_insert_clause. If you specify both, then they can be in either order. | ||
|
||
`merge_insert_clause` | ||
|
||
The merge_insert_clause specifies values to insert into the column of the target table if the condition of the ON clause is false. If you omit the column list after the INSERT keyword, then the number of columns in the target table must match the number of values in the VALUES clause. | ||
|
||
To insert all of the source rows into the table, you can use a constant filter predicate in the ON clause condition. An example of a constant filter predicate is `ON (0=1)`. It recognizes such a predicate and makes an unconditional insert of all source rows into the table. This approach is different from omitting the merge_update_clause. In that case, the database still must perform a join. With constant filter predicate, no join is performed. | ||
|
||
Specify the where_clause if you want the database to execute the insert operation only if the specified condition is true. The condition can refer only to the data source table. Database skips the insert operation for all rows for which the condition is not true. | ||
|
||
You can specify this clause by itself or with the merge_update_clause. If you specify both, then they can be in either order. | ||
|
||
|
||
## Examples | ||
|
||
Create tables `target` and `source`: | ||
|
||
```sql | ||
CREATE TABLE target (tid integer, balance integer); | ||
CREATE TABLE source (sid integer, delta integer); | ||
``` | ||
|
||
Add rows to both the tables: | ||
|
||
```sql | ||
# Insert rows into target table | ||
INSERT INTO target VALUES (1, 0); | ||
INSERT INTO target VALUES (2, 20); | ||
INSERT INTO target VALUES (3, 0); | ||
|
||
# Insert rows into source table | ||
INSERT INTO source VALUES (1, 100); | ||
INSERT INTO source VALUES (2, 200); | ||
INSERT INTO source VALUES (3, 300); | ||
INSERT INTO source VALUES (4, 100); | ||
INSERT INTO source VALUES (5, 300); | ||
INSERT INTO source VALUES (6, 600); | ||
``` | ||
|
||
This example shows how to use the WHERE condition for conditional UPDATE and INSERT statements in the MERGE statement: | ||
|
||
```sql | ||
BEGIN; | ||
MERGE INTO target t | ||
USING source s | ||
ON (t.tid = s.sid) | ||
WHEN MATCHED THEN | ||
UPDATE SET balance = s.delta WHERE balance = 0 | ||
WHEN NOT MATCHED THEN | ||
INSERT VALUES (s.sid, s.delta) WHERE s.sid >= 5; | ||
``` | ||
|
||
This example shows how to use PostgreSQL style UPDATE and conditional INSERT in the MERGE statement: | ||
|
||
```sql | ||
BEGIN; | ||
MERGE INTO target t | ||
USING source s | ||
ON (t.tid = s.sid) | ||
WHEN MATCHED AND balance > 0 THEN | ||
UPDATE SET balance = s.delta | ||
WHEN NOT MATCHED THEN | ||
INSERT VALUES (s.sid, s.delta) WHERE s.sid >= 5; | ||
``` | ||
|
||
This example shows how to use conditional UPDATE and PostgreSQL style UPDATE in the MERGE statement: | ||
|
||
```sql | ||
BEGIN; | ||
MERGE INTO target t | ||
USING source s | ||
ON (t.tid = s.sid) | ||
WHEN MATCHED THEN | ||
UPDATE SET balance = s.delta WHERE balance > 0 | ||
WHEN NOT MATCHED AND s.sid >= 5 THEN | ||
INSERT VALUES (s.sid, s.delta); | ||
``` | ||
|
||
This example shows how to use the WHERE condition for UPDATE and DELETE statements, and also WHEN MATCHED THEN DELETE: | ||
|
||
```sql | ||
MERGE INTO target t | ||
USING source s | ||
ON (t.tid = s.sid) | ||
WHEN MATCHED THEN | ||
UPDATE SET t.balance = t.balance + 100 WHERE t.balance = 20 | ||
DELETE WHERE t.balance = 30 | ||
WHEN NOT MATCHED THEN | ||
INSERT VALUES (s.sid, s.delta) | ||
WHEN MATCHED THEN | ||
DELETE; | ||
``` |
Oops, something went wrong.
12e8296
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π Published on https://edb-docs-staging.netlify.app as production
π Deployed on https://641cc16746a9760bd7fdd3d6--edb-docs-staging.netlify.app
12e8296
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π Published on https://edb-docs.netlify.app as production
π Deployed on https://641cc6b2f4405f0b87d1bdb6--edb-docs.netlify.app