# MERGE `MERGE` is a statement that conditionally performs `INSERT`, `UPDATE`, or `DELETE` operations. It modifies the content of the ``, merging data from the `` and static `` sources. First, the MERGE command performs an outer join from `` to `` using `ON ` producing candidate change rows. For each candidate change row, the `MATCHED` or `NOT MATCHED` status is determined. If applicable, `NOT MATCHED` on `` or `` is set. Finally, for each candidate change row, the first `WHEN` clause under the applicable `MATCHED`/`NOT MATCHED` condition is executed. A `WHEN` clause without `AND ` implies unconditional execution. Subsequent `WHEN` clauses for the same target/source matching condition are not allowed. If no `WHEN` clause evaluates as true, the target row remains unchanged, which is equivalent to specifying `NOP`. `MERGE` actions have the same effect as the standard `UPDATE`, `INSERT`, or `DELETE` commands. `MERGE` can update the contents of an existing target ``, produce a new `
`, or produce a new virtual ``. When `MERGE INTO` is specified or implied, `` must be a base `
` and contents are updated in place. `PRODUCING NEW` may not be specified. When `MERGE FROM` is specified, `PRODUCING NEW` must also be specified. `` can be a base `
` or any virtual table (i.e. `` or `PASS-THRU` ``). If `` is specified, it will be created as a new `
` and populated in the same way as when `` is updated with `MERGE INTO`. The output ``'s row type will correspond to the row type of ``. And its primary index (in the case when `` is produced) will correspond to the primary index of ``. The ``'s ``s are not replicated. If the resulting virtual-table row type is a union type, then the output must be a virtual-table `PASS-THRU`, not an update to `` or creation of `` as base `
`. ``` ::= MERGE [ { INTO | FROM } ] [ [ AS ] ] [ PRODUCING NEW ] USING [ [ AS ] ] [ [ SCALAR ] [ ,...n ] ] [ ON ] [ WHEN MATCHED [ AND ] THEN ] [ ...n ] [ WHEN NOT MATCHED [ BY TARGET ] [ AND ] THEN ] [ ...n ] [ WHEN NOT MATCHED BY SOURCE [ AND ] THEN ] [ ...n ] ``` ``` ::= ::= ::= ::= ::= ::= ``` ``` ::= { UPDATE [ SET ] { = } [ ,...n ] | DELETE | NOP } ``` Specifies the update or delete action that is applied to all rows of `` that don't match the rows returned by `` ON ``, and which satisfy any additional predicate. **``** Identifies column in ``. Each column may be referenced once. **``** Aura must match corresponding aura in ``. **DELETE** Delete the matched target row. **NOP** No operation performed. ``` ::= INSERT [ ( [ ,...n ] ) ] VALUES ( [ ,...n ] ) | NOP ``` **``** Identifies column in ``. Each column may be referenced once. **``** Aura must match corresponding aura in ``. The count out `INSERT` columns and `VALUES` must match. **NOP** No operation performed. ## API ``` +$ merge $: %merge target-table=table-set new-table=(unit table-set) source-table=table-set predicate=predicate matched=(list matching) unmatched-by-target=(list matching) unmatched-by-source=(list matching) == ``` ## Arguments **`[ { INTO | FROM } ] [ [ AS ] ]`** `` is alternative name to reference `` in `WHEN` clauses and predicates. If `{ INTO | FROM }` is not specified, `INTO` is the default. If `INTO` is specified (or implied) then `` is a base-table If `` is a virtual-table -- any `` other than a base-table, i.e. qualified ``, ``, `*`, or `( column-1 [,...column-n] )` -- then `FROM` is required. `INTO` must not accompany `PRODUCING NEW` argument. `FROM` must accompany `PRODUCING NEW` argument. `` is the table, view, or CTE against which the data rows from `` are matched based on ``. If `FROM` is specified, any `INSERT`, `UPDATE`, or `DELETE` operations specified by the `WHEN` clauses, as well as matched but otherwise unaffected target table rows, produce a new `` as specified by the `PRODUCING NEW` clause. **`[ PRODUCING NEW ` ]** Required when `FROM` is specified. Prohibited when `INTO` is specified or implied. If `` has the syntax of a qualified `
`, it cannot already exist. If `` has a row type which is a union type, `` cannot be a base `
`. **`USING [ [ AS ] ]`** Specifies the data source that is matched with the data rows in `` joining on ``. `` can be any ``. `` is an alternative name to reference `` in `WHEN` clauses and predicates. **`[ [ SCALAR ] [ ,...n ] ]`** TBD **`ON `** Specifies the conditions on which `` joins with ``, determining the matching and can be any valid `` not resulting in cartesian join. If `` is not specified, source and target must share row type and matching implies rows equal by value. If `` does not resolve for any row sub-type between the target and source it potentially creates `NOT MATCHED` conditions `BY TARGET` and `BY SOURCE` on rows that otherwise are equal by value. **`[ WHEN MATCHED [ AND ] THEN ] [ ...n ]`** Specifies that all rows of ``, which join the rows returned by `` ON `` or the implied join when `ON` predicate not present, and satisfy `` (when present), result in some action according to the `` clause. `WHEN MATCHED` clause without `AND ` implies unconditionally apply the `` action. If two or more `WHEN MATCHED` clauses are specified only the last clause may be unaccompanied by `AND `. The first `` evaluating to true determines the `` action. If there is no unconditional `` action, it is the same as specifying `NOP` for unconditional action. **`[ WHEN NOT MATCHED [ BY TARGET ] [ AND ] THEN ] [ ...n ]`** Specifies the action on `` for every row returned by `` ON `` that doesn't match a row in target-table, but satisfies ``, if present. The action to take is specified by the `` clause. `WHEN NOT MATCHED BY TARGET` clause without `AND ` implies unconditionally apply the `` action. If two or more `WHEN NOT MATCHED BY TARGET` clauses are specified only the last clause may be unaccompanied by `AND `. The first `` evaluating to true determines the `` action. If there is no unconditional `` action, it is the same as specifying `NOP` for unconditional action. **`WHEN NOT MATCHED BY SOURCE [ AND ] THEN `** Specifies that all rows of ``, which don't match the rows returned by `` ON ``, and that satisfy any additional search condition, are updated or deleted according to the `` clause. `WHEN NOT MATCHED BY SOURCE` clause without `AND ` implies unconditionally apply the `` action. If two or more `WHEN NOT MATCHED BY SOURCE` clauses are specified only the last clause may be unaccompanied by `AND `. The first `` evaluating to true determines the `` action. If there is no unconditional `` action, it is the same as specifying `NOP` for unconditional action. When no rows are returned by ``, columns in the source table can't be accessed, and therefore the `` action cannot reference columns in ``. ## Remarks When `` is updated in place or `` specified as a base `
`, the command potentially results in a state change of the Obelisk agent. Cross ship merges are not allowed. In the case of multiple `WHEN MATCHED` or `WHEN NOT MATCHED` and overlapping predicates, the cases are processed in order, so the first successful case takes precedence. Tables in the namespace *sys* cannot be merged into. At least one of the three `MATCHED` / `NOT MATCHED` clauses must be specified, but they can be specified in any order. `INSERT`, `UPDATE`, or `DELETE` actions specified on `` are limited by any constraints defined on it (when it is a base `
`), including unique indices and any cascading referential integrity constraints. It `` is updated in place, or `` created, every `INSERT` clause must account for all columns in ``. Inserting fewer columns results in a new row sub-type, which is allowed when creating a virtual ``. Any `` referencing a column each from `` and `` satisfies the requirement that `ON ` not produce a cartesian join. However, it is to be noted a cartestian join cannot be entirely prevented depending on column contents. ## Produced Metadata `@@ROWCOUNT` returns the total number of rows [inserted=@ud updated=@ud deleted=@ud]. ## Exceptions `` does not exist `GRANT` permission on `` violated `` does not exist `GRANT` permission on `` violated `` already exists referential integrity violation on the updated `` unique key violation -- for updateable `` unique key violation is a violation of the primary index or any other unique index defined on the table -- for producing new base `
` output, `` primary index determines unique key violations -- for producing `PASS-THRU` output, columns referenced in `` determine unique key violations for row sub-types that include all of the referenced columns -- for producing `PASS-THRU` output when `` is not present, or the produced row sub-type does not include all of the referenced columns, the entire row by value determines unique key violations