DataBase/oracle

oracle PL/SQL_서브프로그램_함수

HUCHUCHU 2020. 4. 20. 02:32

 

< 함수와 프로시저의 차이점 >

프로시저 : in, out, in out모드를 사용여 값을 반환 하기도 하고, 반환하지 않고 종료 할 수 있다.

함수 : 작업을 수행한 후에 결과를 반환한다

 

   ***함수의 구문 형식

create [or replace ] function 함수명

파라미터1 데이터타입,

파라미터2 데이터타입,

...

return datatype

is [as]

PL/SQL 블럭;

 

기본함수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
create or replace function dept_max_sal
    ( dept_id employees.department_id%type)
 
return number
is
    max_sal employees.salary%type;
begin
    select max(salary) into max_sal
    from employees
    where department_id = dept_id;
    return max_sal;
end;
/
 
select dept_max_sal(50from dual;
 

함수_2

1
2
3
4
5
6
7
8
9
10
11
12
create or replace function cnt_member
    (cnt number)
return number
is
    total_cnt number;
begin
    select count(*into total_cnt
    from employees
    where department_id = cnt;
    return total_cnt;
end;
/
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
1
select cnt_member(50from employees;

==> 107개가 출력된다

1
select count(*from employees where department_id = 50;

==> 이거랑 같은 값이 나와야하는거 아닌가??