|
| 1 | +========== |
| 2 | +MERGE INTO |
| 3 | +========== |
| 4 | + |
| 5 | +Synopsis |
| 6 | +-------- |
| 7 | + |
| 8 | +.. code-block:: text |
| 9 | +
|
| 10 | + MERGE INTO target_table [ [ AS ] target_alias ] |
| 11 | + USING { source_table | query } [ [ AS ] source_alias ] |
| 12 | + ON search_condition |
| 13 | + WHEN MATCHED THEN |
| 14 | + UPDATE SET ( column = expression [, ...] ) |
| 15 | + WHEN NOT MATCHED THEN |
| 16 | + INSERT [ column_list ] |
| 17 | + VALUES (expression, ...) |
| 18 | +
|
| 19 | +Description |
| 20 | +----------- |
| 21 | + |
| 22 | +The ``MERGE INTO`` statement inserts or updates rows in a ``target_table`` based on the contents of the ``source_table``. |
| 23 | +The ``search_condition`` defines a relation between the source and target tables. |
| 24 | +When the condition is met, the target row is updated. When the condition is not met, a new row is inserted into the target table. |
| 25 | +In the ``MATCHED`` case, the ``UPDATE`` column value expressions can depend on any field of the target or the source. |
| 26 | +In the ``NOT MATCHED`` case, the ``INSERT`` expressions can depend on any field of the source. |
| 27 | + |
| 28 | +The ``MERGE INTO`` command requires each target row to match at most one source row. An exception is raised when a single target table row matches more than one source row. |
| 29 | +If a source row is not matched by the ``WHEN`` clause and there is no ``WHEN NOT MATCHED`` clause, the source row is ignored. |
| 30 | + |
| 31 | +The ``MERGE INTO`` statement is commonly used to integrate data from two tables with different contents but similar structures. |
| 32 | +For example, the source table could be part of a production transactional system, while the target table might be located in a data warehouse for analytics. |
| 33 | +Regularly, MERGE operations are performed to update the analytics warehouse with the latest production data. |
| 34 | +You can also use MERGE with tables that have different structures, as long as you can define a condition to match the rows between them. |
| 35 | + |
| 36 | +Example |
| 37 | +------- |
| 38 | + |
| 39 | +Update the sales information for existing products and insert the sales information for the new products in the market. |
| 40 | + |
| 41 | +.. code-block:: text |
| 42 | +
|
| 43 | + MERGE INTO product_sales AS s |
| 44 | + USING monthly_sales AS ms |
| 45 | + ON s.product_id = ms.product_id |
| 46 | + WHEN MATCHED THEN |
| 47 | + UPDATE SET |
| 48 | + sales = sales + ms.sales |
| 49 | + , last_sale = ms.sale_date |
| 50 | + , current_price = ms.price |
| 51 | + WHEN NOT MATCHED THEN |
| 52 | + INSERT (product_id, sales, last_sale, current_price) |
| 53 | + VALUES (ms.product_id, ms.sales, ms.sale_date, ms.price) |
| 54 | +
|
| 55 | +Limitations |
| 56 | +----------- |
| 57 | + |
| 58 | +Any connector can be used as a source table for a ``MERGE INTO`` statement. |
| 59 | +Only connectors which support the ``MERGE INTO`` statement can be the target of a merge operation. |
| 60 | +See the :doc:`connector documentation </connector/>` for more information. |
0 commit comments