0

why am I getting this error?

Question) write a trigger to restrict the insertion of commission more than salary.

prog,

SET SERVEROUTPUT ON;
CREATE OR REPLACE TRIGGER EMP2_TR
BEFORE INSERT ON EMP2
FOR EACH ROW WHEN (NEW.COMM > NEW.SAL)
BEGIN
    Raise_Application_Error (-20100, 'COMMISION MUST NOT BE GREATER THEN SALARY');      
END;
/

error,

SQL> INSERT INTO EMP2 VALUES(101,'RAMAN',100,1000);
INSERT INTO EMP2 VALUES(101,'RAMAN',100,1000)
            *
ERROR at line 1:
ORA-20100: COMMISION MUST NOT BE GREATER THEN SALARY
ORA-06512: at "AYUSH.EMP2_TR", line 2
ORA-04088: error during execution of trigger 'AYUSH.EMP2_TR'

table,

SQL> DESC EMP2    
 Name                                      Null?    Type    
 ----------------------------------------- -------- -------------------------    
 EMP_NO                                             NUMBER(3)    
 NAME                                               VARCHAR2(10)    
 SAL                                                NUMBER(10,3)    
 COMM                                               NUMBER(10,2)
3
  • We don't know the emp2 columns... Commented Mar 19, 2019 at 14:11
  • SQL> DESC EMP2 Name Null? Type ----------------------------------------- -------- ---------------------------- EMP_NO NUMBER(3) NAME VARCHAR2(10) SAL NUMBER(10,3) COMM NUMBER(10,2) Commented Mar 19, 2019 at 14:18
  • You try to insert commission 1000 when salary 100 - so that behavior is just as expected. Commented Mar 19, 2019 at 14:45

2 Answers 2

1

Your insert does not specify the order of the values, so the order of the columns in the table is used. Most probably the sal column is defined before the comm column.

You need to list the columns in the insert statement:

insert into emp2 
  (empid, name, comm, sal) 
values 
  (101,'RAMAN',100,1000);

so that Oracle knows which value belongs to which column.

This is good coding practice anyway

Sign up to request clarification or add additional context in comments.

Comments

0

The question is: Why are you doing this with a trigger?

You should be doing this with a constraint:

alter table emp2 add constraint chk_emp2_comm_sal check (comm <= sal);

6 Comments

Actually there is a question in my practical book asking to do this using trigger.
@AyushmanBokde . . . Your book should catch up with the times and show you how to do this correctly using constraints.
well that's true, but can it be done using trigger.
@GordonLinoff: this seems to be a common practice in PL/SQL examples: using PL/SQL for stuff that should be done more efficiently in a different way. I never understood the reasoning behind examples like that. What's even worse are triggers to ensure referential integrity. I guess the teachers simply don't want to be bothered with coming up with real world examples.
@a_horse_with_no_name . . . I even get the didactic point . . . assuming it is phrased as "Let's implement this check constraint using a trigger". That I can get. But not "Solve this problem using a trigger." That is close to saying "Solve this other problem using cursors (when they are not needed)".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.