-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPLSQL_28.sql
More file actions
22 lines (22 loc) · 917 Bytes
/
Copy pathPLSQL_28.sql
File metadata and controls
22 lines (22 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
drop FUNCTION GET_AVG_SAL;CREATE OR REPLACE FUNCTION get_avg_sal (p_dept_id departments.department_id%type) RETURN number AS
v_avg_sal number;
BEGIN
select avg(salary) into v_avg_sal from employees where department_id = p_dept_id;
RETURN v_avg_sal;
END get_avg_sal;
----------------- using a function in begin-end block
declare
v_avg_salary number;
begin
v_avg_salary := get_avg_sal(50);
dbms_output.put_line(v_avg_salary);
end;
----------------- using functions in a select clause
select employee_id,first_name,salary,department_id,get_avg_sal(department_id) avg_sal from employees;
----------------- using functions in group by, order by, where clauses
select get_avg_sal(department_id) from employees
where salary > get_avg_sal(department_id)
group by get_avg_sal(department_id)
order by get_avg_sal(department_id);
----------------- dropping a function
drop function get_avg_sal;