Example; Reflexive Updates - HP Neoview SQL Reference Manual

Hide thumbs Also See for Neoview SQL:
Table of Contents

Advertisement

Example

This query extracts derived columns 'a' and 'b' from the USING query as derived table 'z' and
use each row to join to the merged table 't' based on the ON clause. For each matched row, column
'b' in table 't' is updated using column 'b' in derived table 'z'. For rows that are not matched,
values z.a and z.b are inserted.
MERGE INTO t ON a = z.a
USING (SELECT * FROM t1) z(a,b)
WHEN MATCHED THEN UPDATE SET b = z.b
WHEN NOT MATCHED THEN INSERT VALUES (z.a, z.b);

Reflexive Updates

A reflexive update enables you to incrementally update some columns if the row exists and insert
other values if the row does not exist.
Example
This example is a reflexive update using rowsets. It incrementally updates column 'b' if the row
exists and inserts a new row, if the row does not exist.
MERGE INTO t ON a = ?[10]
WHEN MATCHED THEN UPDATE SET (b,c) = (b + ?[10], ?[10])
WHEN NOT MATCHED THEN INSERT VALUES (?[10], ?[10], ?[10]);
This is an example of a reflexive update from one table into another.
MERGE INTO t4 USING (SELECT * FROM t3) AS t3
ON t4.a = t3.a
WHEN MATCHED THEN UPDATE SET (b,c) = (b + t3.b, t3.c)
WHEN NOT MATCHED THEN INSERT VALUES (t3.a, t3.b, t3.c);
MERGE INTO Statement
131

Advertisement

Table of Contents
loading

Table of Contents