It seems like I am able to reference a column the target table in a case predicate for a matchedClause in a MERGE command, but am unable to do so in a notMatchedClause.
For example I create two tables and insert some values to them as below.
create table test_tab_a (
name string,
something string
);
create table test_tab_b (
name string,
something string
);
insert into test_tab_a values ('a', 'b');
insert into test_tab_a values ('c', 'z');
insert into test_tab_b values ('a', 'c');
insert into test_tab_b values ('c', 'z');
Then run a merge command as below and works just fine.
merge into public.test_tab_a as target
using (
select * from public.test_tab_b
) src
on target.name = src.name
when matched and target.SOMETHING = src.something then delete;
However when I run a command using a not matched clause, I get an invalid identifier error.
merge into public.test_tab_a as target
using (
select * from public.test_tab_b
) src
on target.name = src.name
when not matched and b.SOMETHING != a.something then insert values (name, something);
Why is the case_predicate evaluated differently depending on the type of clause?