I am trying to create a pl/sql function to calculate the total of all salaries in an employee table and return it. I am using the sum function but i am getting an error
I have tried changing the sql statement to this solution provided a stack overflow
SELECT (select sum(salary) from employee) INTO total_sum FROM dual;
Below is a sum statement working
SQL> select sum(salary) from employee;
SUM(SALARY)
-----------
281000
Here is the Pl/SQL Function
CREATE OR REPLACE FUNCTION total_salary(dep_number IN NUMBER)
RETURN NUMBER
IS
total_sum NUMBER;
BEGIN
SELECT (select total_sum sum(salary) from employee) INTO total_sum FROM dual;
return total_sum;
END total_salary;
Here is the error Warning: Function created with compilation errors.
SQL> show errors
Errors for FUNCTION TOTAL_SALARY:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/1 PL/SQL: SQL Statement ignored
6/29 PL/SQL: ORA-00923: FROM keyword not found where expected
I expect the function to return the sum of the salary
Try this:
CREATE OR REPLACE FUNCTION total_salary(dep_number IN NUMBER)
RETURN NUMBER
IS total_sum NUMBER;
BEGIN
SELECT SUM(salary)
INTO total_number
FROM employee
WHERE deptid = dep_number;
RETURN(total_sum);
END;