sql은 다른 프로그래밍 언어와는 많이 다르지만 MySQL에서 프로그래밍 기능이 필요하다면 스토어드 프로시저를 이용하여 sql 프로그래밍을 사용할 수 있다.
if문
조건문으로 가장 많이 사용되는 프로그래밍 문법이다.
if문의 기본 형식
조건식이 참이라면 'sql 문장들'을 실행하고, 그렇지 않으면 아무것도 실행되지 않는다.
if <조건식> then
sql 문장들
end if;
만약 위의 'sql 문장들' 이 한 문장만 사용한다면 문장만 작성해도 되지만 두 문장 이상이라면 begin ~ end 로 묶어서 사용해야 한다.
delimiter $$
create procedure ifProc1()
begin
if 100 = 100 then
select '100은 100과 같습니다.';
end if;
end $$
delimiter ;
call ifProc1();
if-else문
조건에 따라 다른 부분을 실행할 때 주로 사용한다.
drop procedure if exists ifProc2;
delimiter $$
create procedure ifProc2()
begin
declare myNum int; -- myNum 변수 선언, 변수의 데이터 형식은 int
set myNum = 200; -- myNum에 200을 할당
if myNum = 100 then -- myNum이 100이라면
select "100입니다.";
else
select "100이 아닙니다.";
end if;
end $$
delimiter ;
call ifProc2();
case문
여러 가지 조건 중에서 선택해야 하는 다중 분기에서 사용한다.
case문의 기본 형식
case
when 조건1 then
sql 문장1
when 조건2 then
sql 문장2
...
else
sql 문장n
end case;
case와 end case 사이에는 여러 조건들을 넣을 수 있다.
조건이 여러개라면 when을 여러번 반복하고 모든 조건에 해당하지 않으면 마지막에 작성된 else 부분을 수행하게 된다.
case 사용 예제1
학점을 부여할 때 90점 이상은 A, 80점 이상은 B, 70점 이상은 C, 60점 이상은 D, 60점 미만은 F를 처리하는 sql 작성하기
drop procedure if exists caseProc;
delimiter $$
create procedure caseProc()
begin
declare point int;
declare credit char(1); -- 학점을 저장할 변수
set point = 88;
case
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;
select concat("취득 점수 : ", point, "점"), concat("학점 : ", credit);
end $$
delimiter ;
call caseProc();
while문
필요한 만큼 계속 같은 내용을 반복할 때 주로 사용한다.
조건식이 참인 경우 'sql 문장들' 을 계속 반복한다.
while문의 기본 형식
while <조건식> do
sql 문장들
end while;
while문을 이용하여 1부터 100까지의 값을 모두 더하는 기능 구현하기
drop procedure if exists whileProc;
delimiter $$
create procedure whileProc()
begin
declare i int; -- 1 에서 100까지 증가할 변수
declare hap int; -- 더한 값을 누적할 변수
set i = 1;
set hap = 0;
while(i <= 100) do
set hap = hap + i; -- hap의 원래 값에 i를 더해서 다시 hap에 넣기
set i = i + 1; -- i의 원래값에 1을 더해서 i의 값에 넣기
end while; -- i가 100이하인 동안에 계속 반복
select '1부터 100까지의 합==>', hap;
end $$
delimiter ;
call whileProc();
while 문의 응용
- iterate [레이블] : 지정한 레이블로 다시 돌아가 계속 진행하는 것, 파이썬의 continue와 유사한 기능이다.
- leave [레이블] : 지정한 레이블을 빠져나가는 것, 파이썬의 reak와 유사한 기능이다.
1에서 100까지의 합계에서 4의 배수를 제외하고 합계가 1000이 넘으면 더하는 것을 그만두고 1000이 넘는 순간 숫자를 출력한 후 프로그램을 종료하는 sql 작성하기
drop procedure if exists whileProc2;
delimiter $$
create procedure whileProc2()
begin
declare i int; -- 1에서 100까지 증가할 변수
declare hap int; -- 더한 값에 누적할 변수
set i = 1;
set hap = 0;
myWhile: -- while문을 myWhile이라는 레이블로 지정
while (1 <= 100) do
if(i % 4 = 0) then
set i = i + 1;
iterate myWhile; -- 지정한 레이블로 돌아가서 다시 진행(continue)
end if;
set hap = hap + i; -- i가 4의 배수가 아니면 hap에 누적
if (hap >= 1000) then
leave myWhile; -- 지정한 label문을 떠남, while문 종료(break)
end if;
set i = i + 1;
end while;
select '1부터 100까지의 합(4의 배수 제외), 1000 넘으면 종료 -->', hap;
end $$
delimiter ;
call whileProc2();'데이터베이스' 카테고리의 다른 글
| MySQL 테이블 (1) | 2023.05.26 |
|---|---|
| MySQL 동적sql (0) | 2023.05.26 |
| MySQL 스토어드 프로시저 (1) | 2023.05.25 |
| MySQL 데이터 형 변환 (1) | 2023.05.25 |
| MySQL 변수 (1) | 2023.05.25 |