I am using merge command to insert a non-exist record into table. When I use simple insert command it works fine. If I use merge system always alert ORA-00904: “T”.”GROUP_COMPANY_ID” invalid identifier. Once I change the ON condition to (1=1) to force be true then the merge command works fine.
What the original merge statement goes to wrong? I am very sure the table was created without double quoted name so there is not case issue here.
create table test
(
create_date DATE not null,
group_company_id CHAR(16) not null
)
-- This is okay
INSERT INTO test (create_date, group_company_id) VALUES (TO_DATE('20100531', 'YYYYMMDD'), 'abc');
-- This one will raise ORA-00904 error
MERGE INTO test T
USING (SELECT 'abc' AS group_company_id FROM DUAL) C
ON (T.group_company_id = C.group_company_id)
-- ON (1 = 1)
WHEN NOT MATCHED THEN
INSERT (create_date, group_company_id)
VALUES (TO_DATE('20100531', 'YYYYMMDD'), 'abc')
WHEN MATCHED THEN
UPDATE SET group_company_id = 'abc';
Qualify the column names with the table aiases in the
insertandupdateclausesEdit: