6.6. MERGE

Used for

Merging data from a source set into a target relation

Available in

DSQL, PSQL

Syntax

  1. MERGE INTO target [[AS] target-alias]
  2. USING <source> [[AS] source-alias]
  3. ON <join-condition>
  4. [ WHEN MATCHED
  5. THEN UPDATE SET colname = <value> [, <colname> = <value> ...]]
  6. [ WHEN NOT MATCHED
  7. THEN INSERT [(<columns>)] VALUES (<values>)]
  8. <source> ::= tablename | (<select-stmt>)
  9. <columns> ::= colname [, colname ...]
  10. <values> ::= <value> [, <value> ...]
Table 76. 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

tablename

Table or view name

select-stmt

Select statement of the derived table

colname

Name of a column in the target relation

value

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

Description

The MERGE statement merges data into a 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, or neither.

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

Notes

At least one WHEN clause must be present. Only one of each WHEN clause can be supplied. This will change in the next major version of Firebird, when compound matching conditions will be supported.

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 that 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 Tracker ticket CORE-4400.

ALERT : Another irregularity!

If the WHEN MATCHED clause is present and several records match a single record in the target table, an UPDATE will be executed on that one target record for each one of the matching source records, with each successive update overwriting the previous one. This behaviour does not comply with the SQL:2003 standard, which requires that this situation throw an exception (an error).

Examples

  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);
  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);
  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);
The “Unstable Cursor” Problem

Because of the way the execution of data-changing DML is implemented in Firebird, up to and including this version, the sets targeted for merging sometimes produce unexpected results. For more information, refer to The Unstable Cursor Problem in the UPDATE section.