I have following MERGE statement. Table table_A contains list of ID (cli_id) and column fb_flag. The query inside update the column fb_flag in table_A when cli_id is matching. Which works.
However when I want insert something else when the IDs are not matching I have difficulties.
See my two attempts in comments.
MERGE INTO tab_A chi
USING (SELECT cli_id,
'TRUE' AS free_book_b
FROM tab_freebook d) fb
ON (chi.cli_id = fb.cli_id)
WHEN MATCHED THEN
UPDATE SET chi.fb_flag = fb.free_book_b;
--first attempt
/* WHEN NOT MATCHED THEN
UPDATE SET chi.fb_flag = 'FALSE'; */
--second attempt
/* WHEN NOT MATCHED THEN
INSERT (chi.cli_id)
VALUES ('FALSE')
WHERE chi.cli_id IS NULL; */
Thank you for any advice.
EDIT:
Expected result:
tabA
| cli_id | fb_flag |
------------------------
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
tab_freebook
| cli_id | free_book_b |
------------------------
| 23 | TRUE |
| 2 | TRUE |
| 3 | TRUE |
| 6 | TRUE |
What I need have in tabA
| cli_id | fb_flag |
------------------------
| 1 | FALSE |
| 2 | TRUE |
| 3 | TRUE |
| 4 | FALSE |
I have found temporary solution - when I am creating tabA, I fill it with 'FALSE' and then the merge update only the columns where the condition is met.
Which gives me expected result, but it's not really nice and clean as with just merge.
NOT MATCHED BY SOURCE
clause, which Oracle's MERGE probably doesn't support.