/* 패키지 : 연관성이 있는 함수나 프로시저를 그룹으로 모아놓은 개념
패키지의 구성 : 선언부 + 몸체부
-- 참고 - 패키지 선언부에 선언되지 않아도 패키지 몸체부에서 사용할 수 있다...
하지만 권장사항은 아님
-- 선언부
구문 형식
create [or replace] package 패키지명
is[as]
procedure 프로시저명1,
procedure 프로시저명2,
...
end;
-- 몸체부
create [or replace] package body 패키지명
is[as]
procedure 프로시저명1
서브프로그램 바디들... -- 실제 작동하게 될 서브프로그램(프로시저, 함수)
end;
패키지 실행 : 패키지는 여러 호나경에서 호출되어 실행가능
패키지에 대한 실행권한을 가진 사용자만 실행할 수 있음
패키지 삭제 : 몸체부 또는 선언부와 몸체부 모두 삭제 가능
drop package body 패키지명;
drop package 패키지명;
*/
-- 선언부
create or replace package emp_proc
is
procedure emp_sum;
procedure emp_avg;
end;
/
-- 몸체부
create or replace package body emp_proc as
procedure emp_sum
is
-- 서브프로그램 바디
cursor emp_tot_sum
is
select count(*), sum(nvl(salary, 0))
from employees;
tot_num number;
tot_sum number;
begin
open emp_tot_sum;
fetch emp_tot_sum into tot_num, tot_sum;
dbms_output.put_line('전체 인원수 : ' || tot_num || ', 급여합계 : ' || tot_sum);
close emp_tot_sum;
end emp_sum;
procedure emp_avg
is
cursor emp_tot_avg
is
select count(*), avg(nvl(salary, 0))
from employees;
tot_num number;
tot_avg number;
begin
open emp_tot_avg;
fetch emp_tot_avg into tot_num, tot_avg;
dbms_output.put_line('전체 인원수 : ' || tot_num || ', 평균급여 : ' || tot_avg);
close emp_tot_avg;
end emp_avg;
end; -- 패키지의 끝
/
set serveroutput on;
-- 패키지 실행방법 : exec 패키지명.프로시저이름
exec emp_proc.emp_avg;
exec emp_proc.emp_sum;