Check new value before insert and update : Before Update Trigger « Trigger « Oracle PL / SQL

Home
Oracle PL / SQL
1.Aggregate Functions
2.Analytical Functions
3.Char Functions
4.Constraints
5.Conversion Functions
6.Cursor
7.Data Type
8.Date Timezone
9.Hierarchical Query
10.Index
11.Insert Delete Update
12.Large Objects
13.Numeric Math Functions
14.Object Oriented Database
15.PL SQL
16.Regular Expressions
17.Report Column Page
18.Result Set
19.Select Query
20.Sequence
21.SQL Plus
22.Stored Procedure Function
23.Subquery
24.System Packages
25.System Tables Views
26.Table
27.Table Joins
28.Trigger
29.User Previliege
30.View
31.XML
Oracle PL / SQL » Trigger » Before Update Trigger 




Check new value before insert and update
  
SQL>
SQL> CREATE TABLE MyTable (
  2    num_col    NUMBER,
  3    char_col   VARCHAR2(60)
  4    );

Table created.

SQL>
SQL>
SQL> CREATE OR REPLACE TRIGGER OnlyPositive
  2    BEFORE INSERT OR UPDATE OF num_col
  3    ON MyTable
  4    FOR EACH ROW
  5  BEGIN
  6    IF :new.num_col < THEN
  7      RAISE_APPLICATION_ERROR(-20100'Please insert a positive value');
  8    END IF;
  9  END OnlyPositive;
 10  /

Trigger created.

SQL>
SQL> INSERT INTO MyTable (num_col, char_col)
  2    VALUES (1'This is row 1');

row created.

SQL>
SQL> INSERT INTO MyTable (num_col, char_col)
  2    VALUES (-1'This is row -1');
INSERT INTO MyTable (num_col, char_col)
            *
ERROR at line 1:
ORA-20100: Please insert a positive value
ORA-06512: at "JAVA2S.ONLYPOSITIVE", line 3
ORA-04088: error during execution of trigger 'JAVA2S.ONLYPOSITIVE'


SQL>
SQL> drop table MyTable;

Table dropped.

SQL>
SQL>

   
  














Related examples in the same category
1.Create a 'BEFORE UPDATE' Trigger
2.Create a BEFORE UPDATE trigger on the emp table
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.