Wednesday, March 18, 2009

Trigger: Check Values Before Data Entry

This trigger is fired whenever the JOB column is inserted or updated on the EMP table.
Create or Replace Trigger t_Top_Dog
Before Insert or Update
Of Job
On Emp
For Each Row
Begin
If :New.Job = 'PRESIDENT'
Then Raise_Application_Error
(-20111,'Only one top dog allowed.');
End If;
End;
/

Trigger created.

SQL> Insert Into Emp Values
2 (7998,'TOBIAS','DEVELOPER',NULL,SYSDATE,3500,NULL,10);

1 row created.

SQL> Insert Into Emp Values
2 (7999,'ELMO','PRESIDENT',NULL,SYSDATE,6000,NULL,20);
Insert Into Emp Values
*
ERROR at line 1:
ORA-20111: Only one top dog allowed.
ORA-06512: at "SCOTT.T_TOP_DOG", line 3
ORA-04088: error during execution of trigger 'SCOTT.T_TOP_DOG'

No comments:

Post a Comment