본문 바로가기

Database/PLSQL

044 - [Oracle PL/SQL] Package

<샘플코드에서 사용한 데이터는 HR 스키마이고, 오라클 설치시 생성할 수 있는 기본 스키마 입니다>

 

 

 

 

 

 

 

 

 

# create a function to calc square area

create or replace function square_area
    ( p_side number )
    return number
is
begin
    return p_side*p_side;
end;



select square_area(4) from dual;
--------------------------------------------
SQUARE_AREA(4)
--------------
            16

 

 

 

# create a function to calc rectangle area

create or replace function rectangle_area
    ( p_l number,p_w number )
    return number
is
begin
    return p_l*p_w;
end;



select rectangle_area(4,5) from dual;
----------------------------------------------------------
RECTANGLE_AREA(4,5)
-------------------
                 20

 

 

 

# Now because theses 2 functions are logically grouped,
# it is better to use package
# The code will be more organized

create or replace package area
is

    function square_area( p_side number )
    return number;

    function rectangle_area( p_l number,p_w number )
    return number;

--we dont have begin in package specification
end;


create or replace package body area
is
    function square_area( p_side number )
        return number
    is
    begin
        return p_side*p_side;
    end;
    
    function rectangle_area( p_l number,p_w number )
        return number
    is
    begin
        return p_l*p_w;
    end;
--the begin is optional--we use it for initilization
begin
    DBMS_OUTPUT.PUT_LINE('welcome~~');

end;

 

 

 

select area.square_area(4) from dual;
--------------------------------------------------------
AREA.SQUARE_AREA(4)
-------------------
                 16



select area.rectangle_area(4,10) from dual;
--------------------------------------------------------
AREA.RECTANGLE_AREA(4,10)
-------------------------
                       40



begin
    DBMS_OUTPUT.PUT_LINE(area.square_area(4));
end;
--------------------------------------------------------
welcome~~
16

 

 

 

drop function square_area;
--------------------------------------------------------
Function SQUARE_AREA dropped.



drop function rectangle_area;
--------------------------------------------------------
Function RECTANGLE_AREA dropped.