0
Merge /*+ parallel (16)*/ into psx_cluster_audit_t_1 a 
using psx_cluster_cross_ref_t_1  b 
on (
        a.psx_id=b.RECORD_ID
    and a.PSX_BATCH_ID=b.PSX_BATCH_ID
    and b.psx_batch_id='2411121519014999'
)
when matched then update set mpc_new=b.mpc where a.mpc_new is null

The above query giving:

SQL Error [42601]: ERROR: syntax error at or near "where" Position: 234 error in postgresql

I tried with other clauses like ON

2
  • There is no WHERE clause in MERGE. That and the comment in the statement make me believe that you are trying to use a MERGE statement for a different RDBMS. Commented Nov 13, 2024 at 6:54
  • Use valid syntax: postgresql.org/docs/current/sql-merge.html Commented Nov 13, 2024 at 15:18

2 Answers 2

0
MERGE /*+ parallel (16)*/ INTO psx_cluster_audit_t_1 a 
USING psx_cluster_cross_ref_t_1 b 
ON (
    a.psx_id = b.RECORD_ID
    AND a.PSX_BATCH_ID = b.PSX_BATCH_ID
    AND b.psx_batch_id = '2411121519014999'
)
WHEN MATCHED THEN 
    UPDATE SET a.mpc_new = b.mpc 
    WHERE a.mpc_new IS NULL;

try this one

Sign up to request clarification or add additional context in comments.

1 Comment

Tried, but getting the same error
0

You have a couple syntax errors and a totally superfluous/misleading comment. So:

  • Postgres syntax does not accept a where clause within the when matched phase. Move it into the on phase.
  • The update within the when matched phase must refer to only the table named in the into clause; therefore do not alias the column names.
  • Finally this looks like it was originally an Oracle statement. The comment ( /*+ ... */) is how Oracle indicates a hint to its optimizer. In Postgres it's just does nothing and does nothing except perhaps cause confusion.

Taking all this your query becomes:

MERGE  INTO psx_cluster_audit_t_1 a 
USING psx_cluster_cross_ref_t_1 b 
ON (
    a.psx_id = b.RECORD_ID
    AND a.PSX_BATCH_ID = b.PSX_BATCH_ID
    AND b.psx_batch_id = '2411121519014999'
    and a.mpc_new IS NULL      -- where clause from WHEN MATCHED Phased
)
WHEN MATCHED THEN 
    UPDATE SET mpc_new = b.mpc; 

NOTE Not tested. No table description (ddl) nor test data supplied.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.