Function
- 매개변수, return 값
형식
delimiter $$
begin
create function 함수명 (매개변수 자료형) returns 리턴 자료형
select 쿼리처리
return 값 ;
end $$
delimiter ;
-- 1418 error : 권한 설정하는 코드
SET GLOBAL log_bin_trust_function_creators = 1;
권한이 없을 시 설정하는 코드
-- 월급과 커미션을 합친 금액의 세금 계산하는 함수
delimiter $$
create function tax(v_sal decimal(8,2), v_bonus decimal(2,2)) returns double
begin
return v_sal + (ifnull(v_bonus, 0) * v_sal) * 0.15;
end $$
delimiter ;
select first_name, salary + salary * ifnull(commission_pct, 0) as 실급여,
salary * ifnull(commission_pct, 0) as 보너스, tax(salary, commission_pct) as 세금
from employees;
-- 사원번호을 입력하면 업무명을 취득할 수 있는 함수
delimiter $$
create function get_jobName(v_empno int) returns varchar(35)
begin
declare v_jobname varchar(35);
select job_title into v_jobname from employees e, jobs j
where e.job_id = j.job_id and e.employee_id = v_empno;
return v_jobname;
end $$
delimiter ;
set @jobname = get_jobName(103);
select @jobname;
select employee_id, first_name, salary, get_jobName(employee_id) from employees;
procedure와 차이점
- in/out 에 대한 정의가 없다.
- return 값이 반드시 존재해야 한다.
'DB' 카테고리의 다른 글
[MySQL] Sequence (0) | 2022.06.21 |
---|---|
[MySQL] Trigger (0) | 2022.06.21 |
[MySQL] 제어문, Cursor (0) | 2022.06.21 |
[MySQL] PL (0) | 2022.06.21 |
[MySQL] View (0) | 2022.06.20 |