Skip to content
Advertisement

Updating salary on employees for each full year they have been working [closed]

I have to do what i say in the title. I have to update the salary on employees for each full year they have been working. I’m using the HR schema, so you can try it. The code I have right now is like that:

CREATE OR REPLACE FUNCTION new_salary (v_employee_id number) 
RETURN number IS v_result  number;

BEGIN
UPDATE employees SET salary =
        CASE
            WHEN sysdate-hire_date >=1 THEN salary +(salary*0.01)
            ELSE salary
        END
    INTO v_result WHERE employee_id = v_employee_id;

RETURN v_result;

END new_salary;
/

This code isn’s working yet, what more should I do? Also, I don’t know how to say that the salary must multiply more times. Thanks in advance!

Advertisement

Answer

INTO can not be used with the UPDATE statement.

You can use the following code:

CREATE OR REPLACE FUNCTION NEW_SALARY (
    V_EMPLOYEE_ID NUMBER
) RETURN NUMBER IS
    V_RESULT   NUMBER;
BEGIN
    SELECT
        CASE
            WHEN MONTHS_BETWEEN(SYSDATE, HIRE_DATE) / 12 >= 1 
            THEN SALARY + ( SALARY * 0.01 )
                 -- SALARY + ( SALARY * 0.01 * TRUNC(MONTHS_BETWEEN(SYSDATE, HIRE_DATE)/12))
                 -- CODE TO MULTIPLY THE INCREMENT WITH NUMBER OF YEARS.
            ELSE SALARY
        END
    INTO V_RESULT
    FROM
        EMPLOYEES
    WHERE
        EMPLOYEE_ID = V_EMPLOYEE_ID;

    UPDATE EMPLOYEES
    SET
        SALARY = V_RESULT
    WHERE
        EMPLOYEE_ID = V_EMPLOYEE_ID;

    COMMIT;
    RETURN V_RESULT;
END NEW_SALARY;
/

Cheers!!

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement