PL/SQL Basics

WooBuntu·2021년 10월 4일
0

알고쓰자 PL/SQL

목록 보기
1/8

https://www.udemy.com/course/plsql-by-example/learn/lecture/3169046#overview

PL/SQL Structure

DECLARE
BEGIN
EXCEPTION
END;
BEGIN
	dbms_output.put_line("시작합니다")
        -- console.log개념
END;

변수 선언하기

DECLARTE
	변수명 (constant) 변수타입:=디폴트값;
    	변수명 (constant) 변수타입 default 디폴트값;

주석

-- 한 줄짜리 주석

/*
여러 줄짜리 주석
*/

변수 scope

DECLARE
    변수명 변수타입 := 디폴트값;
    -- 전역변수
BEGIN
    DECLARE
        변수명 변수타입 := 디폴트값;
        -- 지역변수
    BEGIN
    END;
END

IF then ELSE

DECLARE
    변수 타입 := 디폴트값;
BEGIN
    if 조건 비교
    then -- 참
    로직 수행;
    else -- 거짓
    로직 수행;
    end if;
END;

예시

DECLARE
    amount := 101;
    discount := 0;
BEGIN
    if amount > 200
    then
    discount := amount * .2;
    elsif amount >= 100 and amount <= 200
    then 
    discount := amount * .1;
    else
    discount := amount * .05;
    end if;
END;

CASE

DECLARE
    amount := 101;
    discount := 0;
BEGIN
    CASE
    WHEN amount > 200
    then
    discount := amount * .2;
    WHEN amount >= 100 and amount <= 200
    then 
    discount := amount * .1;
    else
    discount := amount * .05;
    end CASE;
END;

WHILE

DECLARE
    counter number(2) := 10;
BEGIN
    WHILE counter < 20
    LOOP
        dbms_output.put_line('counter : ' || counter);
        counter := counter + 1;
    END LOOP
END;

FOR

DECLARE
    counter number(2) := 10;
BEGIN
    FOR counter IN (REVERSE) 10..20
    LOOP
        dbms_output.put_line('counter : ' || counter);
        counter := counter + 1;
    END LOOP
END;

0개의 댓글