]> begriffs open source - ai-pg/blob - full-docs/txt/functions-merge-support.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / functions-merge-support.txt
1
2 9.23. Merge Support Functions #
3
4    PostgreSQL includes one merge support function that may be used in the
5    RETURNING list of a MERGE command to identify the action taken for each
6    row; see Table 9.68.
7
8    Table 9.68. Merge Support Functions
9
10    Function
11
12    Description
13
14    merge_action ( ) → text
15
16    Returns the merge action command executed for the current row. This
17    will be 'INSERT', 'UPDATE', or 'DELETE'.
18
19    Example:
20 MERGE INTO products p
21   USING stock s ON p.product_id = s.product_id
22   WHEN MATCHED AND s.quantity > 0 THEN
23     UPDATE SET in_stock = true, quantity = s.quantity
24   WHEN MATCHED THEN
25     UPDATE SET in_stock = false, quantity = 0
26   WHEN NOT MATCHED THEN
27     INSERT (product_id, in_stock, quantity)
28       VALUES (s.product_id, true, s.quantity)
29   RETURNING merge_action(), p.*;
30
31  merge_action | product_id | in_stock | quantity
32 --------------+------------+----------+----------
33  UPDATE       |       1001 | t        |       50
34  UPDATE       |       1002 | f        |        0
35  INSERT       |       1003 | t        |       10
36
37    Note that this function can only be used in the RETURNING list of a
38    MERGE command. It is an error to use it in any other part of a query.