0

Well I have this Procedure:

SET SERVEROUTPUT ON;
CREATE OR REPLACE PROCEDURE check_salary (job_id employees.job_id%TYPE, salary employees.salary%TYPE)
IS
maxSal NUMBER;
minSal NUMBER;
BEGIN
SELECT MAX(salary) INTO maxSal FROM employees WHERE job_id = job_id;
SELECT MIN(salary) INTO minSal FROM employees WHERE job_id = job_id;
IF maxSal >= salary OR minSal <= salary THEN
RAISE_APPLICATION_ERROR(-20001,'Invalid slary '||salary||'. Salaries for job '||job_id||' must be between '||minSal||' and '||maxSal);
END IF;
END;

This PROCEDURE has two parameters. It checks if the salary of a job_id is between the max and the min of the job.

Now I want to create a TRIGGER.

If INSERT or UPDATE is called on employees I want to execute the prodcedure. But I don't know what I should write as a parameter in the procedure

CREATE OR REPLACE TRIGGER check_salary_trg 
BEFORE INSERT OR UPDATE ON employees
FOR EACH ROW
DECLARE
BEGIN
check_salary(); --Don't know which parameter I should use
END;
1
  • Pass :new.job_id and :new.salary but I am not sure if you will get table mutating error or not Commented Jan 5, 2021 at 12:34

2 Answers 2

2

You should use :NEW as New Row and :OLD as Old Row. But I think it is not useful to create a trigger as BEFORE INSERT. You may create a trigger as AFTER INSERT OR UPDATE. Here is an example :

CREATE OR REPLACE TRIGGER check_salary_trg
AFTER INSERT OR UPDATE ON EMPLOYEE
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE
BEGIN
   check_salary(:NEW.job_id);

   EXCEPTION
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END check_salary_trg;
Sign up to request clarification or add additional context in comments.

Comments

0

You just need to add the job_id and the salary

 CREATE OR REPLACE TRIGGER check_salary_trg 
    BEFORE INSERT OR UPDATE ON employees
    FOR EACH ROW
    DECLARE
    BEGIN
    check_salary(:new.job_id, :new.salary);
    END;

3 Comments

The syntax is correct, however you will get an ORA-04091: table HR.employees is mutating, trigger/function may not see it error
@WernfriedDomscheit And how do I avoid that error?
You would need a Compound Trigger or preferably encabsulate all in a stored PL/SQL procedure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.