Check and change new value in a before-insert trigger : Before Event Trigger « Trigger « Oracle PL/SQL Tutorial






SQL>
SQL> CREATE TABLE incremented_values
  2  (value_inserted    NUMBER,
  3   value_incremented NUMBER);

Table created.

SQL>
SQL> CREATE OR REPLACE TRIGGER increment_by_one
  2  BEFORE INSERT ON incremented_values
  3  FOR EACH ROW
  4  BEGIN
  5    :new.value_incremented := :new.value_incremented + 1;
  6  END;
  7  /

Trigger created.

SQL>
SQL> CREATE OR REPLACE TRIGGER increment_by_two
  2  BEFORE INSERT ON incremented_values
  3  FOR EACH ROW
  4  BEGIN
  5    IF :new.value_incremented > 1 THEN
  6      :new.value_incremented := :new.value_incremented + 2;
  7    END IF;
  8  END;
  9  /

Trigger created.

SQL>
SQL> INSERT INTO incremented_values VALUES(1,1);

1 row created.

SQL>
SQL> SELECT * FROM incremented_values;

VALUE_INSERTED VALUE_INCREMENTED
-------------- -----------------
             1                 2

SQL>
SQL>
SQL> DROP TABLE incremented_values;

Table dropped.

SQL>








28.6.Before Event Trigger
28.6.1.BEFORE EVENT triggers, are for preventing the event from actually happening
28.6.2.Insert before trigger
28.6.3.Check and change new value in a before-insert trigger
28.6.4.Fire a trigger before an update of the table
28.6.5.Change data in a before insert or update trigger
28.6.6.BEFORE INSERT OR UPDATE OF id
28.6.7.Check new value in before insert trigger
28.6.8.Check new value in before trigger
28.6.9.convert character values to upper case
28.6.10.Use trigger to create autoincrement column
28.6.11.Implementing Autonumbering Functionality