본문 바로가기
It Study/프로그래밍 공부

데이터베이스 procedure 연습 예제

by prlkt5200 2024. 9. 24.
반응형
-- 만약에 데이터베이스상의 메소드인 procedure가 존재한다면 삭제하라는 의미이다.
drop procedure if exists ifproc;


--delimiter $$(아무 문자나 써도 되고, 문자의 끝을 의미하는 것이 해당 문자라는 의미이다)
--로 시작합니다.
delimiter $$

--procedure ifproc()를 생성한다는 것입니다.
create procedure ifproc()

--sql의 begin end $$는 괄호를 의미합니다.
begin

--변수명 선언 및 초기화입니다.
 declare var1 int;
 set var1 = 100;
 
 if var1 = 100 then
  select '100입니다';
 else 
  select '100이 아닙니다';
--if문 종료
  end if;

--proceduer의 끝을 의미합니다.
end $$
delimiter ;
call ifproc();

 

 

--데이터베이스상의 해당함수가 존재할 시 삭제한다.
drop procedure if exists ifproc2;

use employees;

--//로 procedure를 끝낼 것임을 나타낸다.
delimiter //

--프로시저를 생성한다
create procedure ifproc2()
--프로시저의 괄호를 의미하는 begin과 end
begin

--변수 선언을 해준다. 선언 변수명 변수타입. 여기서는 3개를 해줬다.
declare hiredate date;
declare curdate date;
declare days int;

select from where 문으로 가져온 것을 into로 마지막에 hiredate에 넣어준다.
select hire_date into hiredate
from employees.employees
where emp_no = 10001;

--set으로 해당 변수명에 sql 내장 함수를 이용해서 값을 넣어준다.
set curdate = current_date();
--set으로 해당 변수명에 sql 내장 함수를 이용해서 두 변수의 값을 빼준다.
set days = datediff(curdate,hiredate);

--위의 if식을 통과하면 아래의 select 문으로 결과를 출력하고 end if로 if문을 닫아준 다음
--최종적을 메소드 작성을 마무리 해준다.
if(days/365) >= 5 then 
SELECT CONCAT('입사한지 ', days, '일이나 지났습니다. 축하합니다!') AS '메시지';
ELSE 
SELECT '입사한지 ' + days + '일밖에 안되었네요. 열심히 일하세요.' AS '메시지';
END IF;
END //
DELIMITER ;
CALL ifProc2();

 

 

drop procedure if exists ifproc3;

delimiter //
create procedure ifproc3()
begin
	declare point int;
    declare credit char(1);
	set point = 77;
    
    if point >= 90 then
     set credit = 'a';
	elseif point >= 80 then
     set credit = 'b';
     elseif point >= 70 then
     set credit = 'c';
     ELSEIF point >= 60 THEN 
SET credit = 'D';ELSE 
SET credit = 'F';end if;
 select concat('취득점수==>', point) as '취득점수', concat('학점==>', credit) as '학점';
 end //
 delimiter ;
 call ifproc3();

 

 

DROP PROCEDURE IF EXISTS caseProc;
DELIMITER // 

-- caseProc() 프로시져를 생성합니다
CREATE PROCEDURE caseProc() 

-- begin과 end //로 감싸주고 작성하면 편할 듯 합니다
BEGIN 
-- 변수 선언
DECLARE point INT;
DECLARE credit CHAR(1);
-- 변수에 값을 대입
SET point = 77;

-- java에 switch case문과 비슷하다고 생각하면 됩니다.
CASE 
-- when 조건식 then
WHEN point >= 90 THEN 
SET credit = 'A';
WHEN point >= 80 THEN 
SET credit = 'B';
WHEN point >= 70 THEN 
SET credit = 'C';
WHEN point >= 60 THEN 
SET credit = 'D';
ELSE SET credit = 'F';
-- end case로 마무리 합니다. 마치 if 문을 end if로 끝내듯이.
END CASE;
SELECT CONCAT('취득점수==>', point), CONCAT('학점==>', credit);
END //

-- delimiter로 마무리
DELIMITER ;
CALL caseProc();

 

 

drop procedure if exists whileproc;

delimiter //

create procedure whildproc()
begin

declare i int;
declare hap int;
set i = 1;
set hap = 0;

while (i<= 1000) do
set hap = hap +i;
set i = i+1;
end while;

SELECT hap;
 end //
 
 delimiter ;
 call whileproc();

 

drop procedure if exists whileproc2;


delimiter //

create procedure whileproc2()

begin
declare i int;
declare hap int;
set i =1;
set hap = 0;

-- label을 정해줘야지 iterate:continue leave:break를 사용이 가능하다
mywhile: while(i<=100) do

if(i%7 =0) then 
set i = i+1;
iterate mywhile;
end if;


set hap = hap +i;
if(hap > 1000) then
leave mywhile;
end if;
set i = i + 1;
end while;

select hap;
 end //
delimiter ;
call whileproc2();
반응형