Search code examples
.netoracle-databasemergeora-00933

Determining an Oracle SQL MERGE statement result


Follow up to this question

This (similar version from old link) works in SQL Server 2008, however, Oracle is giving me trouble:

MERGE INTO wdm_test 
USING ( select '10000000000000000000000000000000' Guid from DUAL ) val
ON ( wdm_test.Guid = val.Guid )
WHEN MATCHED THEN UPDATE SET test_column = null
WHEN NOT MATCHED THEN
 INSERT (Guid, test_column)
 VALUES ('10000000000000000000000000000000', null)
OUTPUT $action;

SQL Error: ORA-00933: SQL command not properly ended

Does Oracle not support OUTPUT $action;? If not, is there an alternative?


Solution

  • The MERGE statement does not emit the results, nor does it support the RETURNING clause.

    However, what you could do is flag the records that are affected. Something like:

    when matched update set .... merge_status = 'U'
    when not matched insert ( ...., merge_status, ... ) values ( ...., 'I', .... )
    

    This, of course, requires that you have an additional column to capture the merge status - which isn't always desirable (especially when you only need this information in a transient manner, say to verify the results of the merge).

    Unfortunately, to my knowledge, this is the best you can do.