Postgres insert or update trigger WHEN condition (old)

前端 未结 2 1563
北恋
北恋 2021-01-11 12:17

I need write insert or update trigger, but with WHEN condition with compare OLD and NEW rows.

According documentation OLD is null for insert operation. How i can use

2条回答
  •  死守一世寂寞
    2021-01-11 12:24

    Trigger.oldMap.keySet(); will give the Id's present in the Trigger.oldMap. It is a set type collection of all the Id's. have a look at the following example, change the DML events in the trigger events every time and see the debug logs. you will understand which trigger context variable is available for which DML event.

    CREATE TRIGGER Email_Check_On_Contact before update{ oldMap=new Map(); o = trigger.oldMap; for(Contact newcont: trigger.new) { if(newcont.Email != o.get(newcont.Id).Email) { newcont.Email.addError('Email cannot be changed'); } } }

提交回复
热议问题