A similar question might have been asked, if so please share the link.
Required:
Whenever a new record is created or updated, a validation is required that the salary of the person is in range according to job.
- A Job table with job_id and min,max salary ranges.
- An Employee table with employee_id, name, job_id and salary.
Below is the trigger,
create or replace TRIGGER secure_employees_sal_range
BEFORE INSERT OR UPDATE OF salary,job_id ON employees
FOR EACH ROW
DECLARE
p_min_sal Integer;
p_max_sal Integer;
BEGIN
--check_salaray_range(employee_id,job_id,salary);
select min_salary, max_salary
into p_min_sal, p_max_sal
from jobs
where job_id = p_job_id;
if p_salary < p_min_sal OR p_salary > p_max_sal THEN
RAISE_APPLICATION_ERROR (-20225,
'Salray should in range' || p_min_sal || ' and ' || p_max_sal);
END IF;
END secure_employees_sal_range;
But I am getting error
Error at line 13: PL/SQL: Statement ignored
Line 14: where job_id = :new.job_id;
How can we set the value of p_job_id from employee table?
Update: Now getting the error message when the salary is out of range when creating a new employee
ORA-20225: Salray should in range4000 and 10000 ORA-06512: at "HR.SECURE_EMPLOYEES_SAL_RANGE", line 14 ORA-04088: error during execution of trigger 'HR.SECURE_EMPLOYEES_SAL_RANGE' Unable to process row of table EMPLOYEES.
And when trying to update an existing record then,
ORA-20505: Error in DML: p_rowid=215, p_alt_rowid=EMPLOYEE_ID, p_rowid2=, p_alt_rowid2=. ORA-20225: Salray should in range4000 and 10000 ORA-06512: at "HR.SECURE_EMPLOYEES_SAL_RANGE", line 14 ORA-04088: error during execution of trigger 'HR.SECURE_EMPLOYEES_SAL_RANGE' Unable to process row of table EMPLOYEES.
insertorupdatestatement could have an exception handler that catches the exception thrown by the trigger and displays a more meaningful message to the user. - Justin Cave