UPDATE on INSERT duplicate primary key in Oracle?

后端 未结 3 1014
攒了一身酷
攒了一身酷 2020-12-22 02:07

I have a simple INSERT query where I need to use UPDATE instead when the primary key is a duplicate. In MySQL this seems easier, in Oracle it seems I need to use MERGE.

3条回答
  •  不思量自难忘°
    2020-12-22 02:50

    A typical way of doing this is

    • performing the INSERT and catch a DUP_VAL_ON_INDEX and then perform an UPDATE instead
    • performing the UPDATE first and if SQL%Rows = 0 perform an INSERT

    You can't write a trigger on a table that does another operation on the same table. That's causing an Oracle error (mutating tables).

提交回复
热议问题