I have created a new table VISNHDR with the following fields (it is not the entire table but this is enough for my question):
- ORDNO (VARCHAR 7) - KEY
- ORDDT (NUMERIC 7,0) - KEY
- ORDTM (NUMERIC 6,0) - KEY
- CRTDT (NUMERIC 7,0)
- CRTTM (NUMERIC 6,0)
I am trying to INSERT
a new record to this table and I am trying to prevent its insertion in case of a duplicate key
.
I prefer doing it in one query instead of having one query to check if duplication exists and another one to insert the record.
As per my research on the net, I tried to use MERGE
but with no success.
MERGE INTO visnhdr v1 USING ( SELECT
ordno,
orddt,
ordtm
FROM
visnhdr
WHERE
ordno = 'M12345'
AND orddt = 1170101
AND ordtm = 101010
) AS V2(ordno,orddt,ordtm)
ON (
v1.ordno = v2.ordno
AND v1.orddt = v2.orddt
AND v1.ordtm = v2.ordtm
)
WHEN NOT MATCHED THEN INSERT (
ordno,
orddt,
ordtm,
crtdt,
crttm
) VALUES (
v2.ordno,
v2.orddt,
v2.ordtm,
1170102,
101011
);
I am getting an error:
Row not found for MERGE.
What is the correct syntax to achieve my issue?