PLSQL :NEW and :OLD

前端 未结 13 1166
孤街浪徒
孤街浪徒 2020-12-24 06:30

Can anyone help me understand when to use :NEW and :OLD in PLSQL blocks, I\'m finding it very difficult to understand their usage.

13条回答
  •  一整个雨季
    2020-12-24 07:03

    It's very simple

    If Insert :old = NULL and :New = New Inserted Value
    If Update  :Old = Value already in the table  :New = Updated Value in the Table
    If Delete :Old = Value before deletion :New = NULL
    

    IN other Words

    1. For an INSERT trigger, OLD contains no values, and NEW contains the new values.

    2. For an UPDATE trigger, OLD contains the old values, and NEW contains the new values.

    3. For a DELETE trigger, OLD contains the old values, and NEW contains no values.

    Example:

    CREATE OR REPLACE TRIGGER update_name_view_trigger
    INSTEAD OF UPDATE ON emp_locations
    BEGIN
      UPDATE employees SET
        first_name = substr( :NEW.name, instr( :new.name, ',' )+2),
        last_name = substr( :NEW.name, 1, instr( :new.name, ',')-1)
      WHERE employee_id = :OLD.employee_id;
    END;
    

提交回复
热议问题