I want to update a target table (target
) according to rows in a source table (source
). For that, I use the MERGE
statement. However, I would like the whole MERGE
operation fails on some conditions, but cannot figure how to do.
Example
The target
table contains :
id
==
1
2
The source
table contains :
id | operation
==============
3 | ADD
4 | DELETE
Expected target
table after MERGE
(I don't want any update here because 4
corresponds to DELETE
operation, and since there is no 4
line in target
table, this is considered as an error and MERGE
should fail atomically) :
id
==
1
2
As of now, I use the following request :
MERGE `target` target
USING `source` source
ON target.id = source.id
WHEN not matched AND source.operation = "ADD" THEN
INSERT (id)
VALUES (source.id)
But obviously, I got :
id
==
1
2
3
Is it possible to add in my query a clause like :
WHEN not matched AND source.operation = "DELETE" THEN ERROR("...")
This does not work (ERROR
is unexpected) :
Syntax error: Expected keyword DELETE or keyword INSERT or keyword UPDATE but got identifier "ERROR"
If this is not possible with MERGE
query, is there a way to rewrite it to a similar query to update atomically my target
table as I expect?