6.6 MERGE

Used forMerging data from a source set into a target relation

Available inDSQL, PSQL

Syntax

  1. MERGE INTO target [[AS] target_alias]
  2. USING <source> [[AS] source_alias]
  3. ON <join_condition>
  4. <merge_when> [<merge_when> ...]
  5. [RETURNING <returning_list> [INTO <variables>]]
  6. <merge_when> ::=
  7. <merge_when_matched>
  8. | <merge_when_not_matched>
  9. <merge_when_matched> ::=
  10. WHEN MATCHED [ AND <condition> ] THEN
  11. { UPDATE SET <assignment-list>
  12. | DELETE }
  13. <merge_when_not_matched> ::=
  14. WHEN NOT MATCHED [ AND <condition> ] THEN
  15. INSERT [( <column_list> )] [<override_opt>]
  16. VALUES ( <value_list> )
  17. <source> ::= tablename | (<select_stmt>)
  18. <assignment_list ::=
  19. col_name = <m_value> [, <col_name> = <m_value> ...]]
  20. <override_opt> ::=
  21. OVERRIDING {USER | SYSTEM} VALUE
  22. <column_list> ::= colname [, colname ...]
  23. <value_list> ::= <m_value> [, <m_value> ...]
  24. <m_value> ::= <value_expression> | DEFAULT
  25. <returning_list> ::= * | <output_column> [, <output_column]
  26. <output_column> ::=
  27. target.* | NEW.* | OLD.*
  28. | <return_expression> [COLLATE collation] [[AS] alias]
  29. <return_expression> ::=
  30. <value_expression>
  31. | [target.]col_name
  32. | NEW.col_name
  33. | OLD.col_name
  34. <value_expression> ::=
  35. <literal>
  36. | <context-variable>
  37. | any other expression returning a single
  38. value of a Firebird data type or NULL
  39. <variables> ::=
  40. [:]varname [, [:]varname ...]

Table 6.6.1 Arguments for the MERGE Statement Parameters

ArgumentDescription

target

Name of target relation (table or updatable view)

source

Data source. It can be a table, a view, a stored procedure or a derived table

target_alias

Alias for the target relation (table or updatable view)

source_alias

Alias for the source relation or set

join_conditions

The (ON) condition(s) for matching the source records with those in the target

condition

Additional test condition in WHEN MATCHED or WHEN NOT MATCHED clause

tablename

Table or view name

select_stmt

Select statement of the derived table

col_name

Name of a column in the target relation

value_expression

The value assigned to a column in the target table. This expression may be a literal value, a PSQL variable, a column from the source, or a compatible context variable

return_expression

The expression to be returned in the RETURNING clause Can be a column reference to source or target, or a column reference of the NEW or OLD context of the target, or a value.

ret_alias

Alias for the value expression in the RETURNING clause

varname

Name of a PSQL local variable

The MERGE statement merges records from the source into a target table or updatable view. The source may be a table, view or anything you can SELECT from in general. Each source record will be used to update one or more target records, insert a new record in the target table, delete a record from the target table or do nothing.

The action taken depends on the supplied join condition, the WHEN clause(s), and the - optional - condition in the WHEN clause. The join condition and condition in the WHEN will typically contain a comparison of fields in the source and target relations.

Multiple WHEN MATCHED and WHEN NOT MATCHED clauses are allowed. For each row in the source, the WHEN clauses are checked in the order they are specified in the statement. If the condition in the WHEN clause does not evaluate to true, the clause is skipped, and the next clause will be checked. This will be done until the condition for a WHEN clause evaluates to true, or a WHEN clauses without condition matches, or there are no more WHEN clauses. If a matching clause is found, the action associated with the clause is executed. For each row in the source, at most one action is executed. If the WHEN MATCHED clause is present, and several records match a single record in the target table, an error is raised.

Warning

At least one WHEN clause must be present.

WHEN NOT MATCHED is evaluated from the source viewpoint, that is, the table or set specified in USING. It has to work this way because if the source record does not match a target record, INSERT is executed. Of course, if there is a target record which does not match a source record, nothing is done.

Currently, the ROW_COUNT variable returns the value 1, even if more than one record is modified or inserted. For details and progress, refer to firebird#4722.

6.6.1 The RETURNING Clause

A MERGE statement that affects at most one row can contain a RETURNING clause to return values added, modified or removed. If a RETURNING clause is present and more than one matching record is found, an error multiple rows in singleton select is raised. The RETURNING clause can contain any columns from the target table (or updateable view), as well as other columns (eg from the source) and expressions.

The user executing the statement needs to have SELECT privileges on the columns specified in the RETURNING clause.

The optional INTO sub-clause is only valid in PSQL.

Note

The restriction that RETURNING can only be used with a statement that affects at most one row might be removed in a future version.

Column names can be qualified by the OLD or NEW prefix to define exactly what value to return: before or after modification. The returned values include the changes made by BEFORE triggers.

The syntax of the returning_list is similar to the column list of a SELECT clause. It is possible to reference all columns using *, or *table_name*.*, NEW.* and/or OLD.*.

For the UPDATE or INSERT action, unqualified column names, or those qualified by the target table name or alias will behave as if qualified by NEW, while for the DELETE action as if qualified by OLD.

The following example modifies the previous example to affect one line, and adds a RETURNING clause to return the old and new quantity of goods, and the difference between those values.

Using MERGE with a RETURNING clause

  1. MERGE INTO PRODUCT_INVENTORY AS TARGET
  2. USING (
  3. SELECT
  4. SL.ID_PRODUCT,
  5. SUM(SL.QUANTITY)
  6. FROM SALES_ORDER_LINE SL
  7. JOIN SALES_ORDER S ON S.ID = SL.ID_SALES_ORDER
  8. WHERE S.BYDATE = CURRENT_DATE
  9. AND SL.ID_PRODUCT =: ID_PRODUCT
  10. GROUP BY 1
  11. ) AS SRC (ID_PRODUCT, QUANTITY)
  12. ON TARGET.ID_PRODUCT = SRC.ID_PRODUCT
  13. WHEN MATCHED AND TARGET.QUANTITY - SRC.QUANTITY <= 0 THEN
  14. DELETE
  15. WHEN MATCHED THEN
  16. UPDATE SET
  17. TARGET.QUANTITY = TARGET.QUANTITY - SRC.QUANTITY,
  18. TARGET.BYDATE = CURRENT_DATE
  19. RETURNING OLD.QUANTITY, NEW.QUANTITY, SRC.QUANTITY
  20. INTO : OLD_QUANTITY, :NEW_QUANTITY, :DIFF_QUANTITY

6.6.2 Examples of MERGE

  1. Update books when present, or add new record if absent

    1. MERGE INTO books b
    2. USING purchases p
    3. ON p.title = b.title and p.type = 'bk'
    4. WHEN MATCHED THEN
    5. UPDATE SET b.desc = b.desc || '; ' || p.desc
    6. WHEN NOT MATCHED THEN
    7. INSERT (title, desc, bought) values (p.title, p.desc, p.bought);
  2. Using a derived table

    1. MERGE INTO customers c
    2. USING (SELECT * from customers_delta WHERE id > 10) cd
    3. ON (c.id = cd.id)
    4. WHEN MATCHED THEN
    5. UPDATE SET name = cd.name
    6. WHEN NOT MATCHED THEN
    7. INSERT (id, name) values (cd.id, cd.name);
  3. Together with a recursive CTE

    1. MERGE INTO numbers
    2. USING (
    3. WITH RECURSIVE r(n) AS (
    4. SELECT 1 FROM rdb$database
    5. UNION ALL
    6. SELECT n+1 FROM r WHERE n < 200
    7. )
    8. SELECT n FROM r
    9. ) t
    10. ON numbers.num = t.n
    11. WHEN NOT MATCHED THEN
    12. INSERT(num) VALUES(t.n);
  4. Using DELETE clause

    1. MERGE INTO SALARY_HISTORY
    2. USING (
    3. SELECT EMP_NO
    4. FROM EMPLOYEE
    5. WHERE DEPT_NO = 120) EMP
    6. ON SALARY_HISTORY.EMP_NO = EMP.EMP_NO
    7. WHEN MATCHED THEN DELETE
  5. The following example updates the PRODUCT_INVENTORY table daily based on orders processed in the SALES_ORDER_LINE table. If the stock level of the product would drop to zero or lower, then the row for that product is removed from the PRODUCT_INVENTORY table.

    1. MERGE INTO PRODUCT_INVENTORY AS TARGET
    2. USING (
    3. SELECT
    4. SL.ID_PRODUCT,
    5. SUM (SL.QUANTITY)
    6. FROM SALES_ORDER_LINE SL
    7. JOIN SALES_ORDER S ON S.ID = SL.ID_SALES_ORDER
    8. WHERE S.BYDATE = CURRENT_DATE
    9. GROUP BY 1
    10. ) AS SRC (ID_PRODUCT, QUANTITY)
    11. ON TARGET.ID_PRODUCT = SRC.ID_PRODUCT
    12. WHEN MATCHED AND TARGET.QUANTITY - SRC.QUANTITY <= 0 THEN
    13. DELETE
    14. WHEN MATCHED THEN
    15. UPDATE SET
    16. TARGET.QUANTITY = TARGET.QUANTITY - SRC.QUANTITY,
    17. TARGET.BYDATE = CURRENT_DATE

See alsoSection 6.1, SELECT, Section 6.2, INSERT, Section 6.3, UPDATE, Section 6.4, UPDATE OR INSERT, Section 6.5, DELETE