Mostafa Ibrahem
Mostafa Ibrahem

Reputation: 31

How To Print Returned Value from PL/SQL Function

I have Problem with my code in PL/SQL

I need to return Max(Salary)

Myfunction is:

create or replace
FUNCTION largest_salary
(j_id EMPLOYEES.JOB_ID%TYPE)
return EMPLOYEES.SALARY%TYPE
IS
sal EMPLOYEES.SALARY%TYPE;

begin
select max(salary)
into sal
from employees
where job_id = j_id;
return sal;
end largest_salary;

and when i run this query :

set serveroutput out

Begin
dbms_output.put_line(largest_salary('SA_REP'));
End;

it doesn't show any thing , anyone can Help me get max(salary)?

Note :I Think it returns multiple values but i don't know how to solve it

Upvotes: 1

Views: 8563

Answers (3)

Mohannad_
Mohannad_

Reputation: 89

you must aggregate the query results, then every thing will be ok

select max(salary)
into sal
from employees
where job_id = j_id;
group by job_id;

regards

Upvotes: 0

Kaushik Nayak
Kaushik Nayak

Reputation: 31648

You should use set serveroutput on instead of

set serveroutput out

Upvotes: 4

Lukasz Szozda
Lukasz Szozda

Reputation: 175586

You could use:

SELECT largest_salary('SA_REP')
FROM dual;

It is not printing, but you will get result in grid.

Upvotes: 2

Related Questions