[    ORACLE 조건문(IF,CASE)    ]





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
  [ 제어문 ] : 조건문, 반복문
     
      - 조건문 : if문, case문(switch문과 유사)
      - 반복문 : basic loop문, while문(반복횟수를 정하지 않을 경우)
              : for문(반복횟수 지정할 경우)
 
      // if문 : if ~ end if
            EX)
              if(조건) then
                실행명령;
              elsif(조건) then
                실행명령;
              else 실행명령;
              end if;
       
      // case
            EX)
                case 변수명
                  when 값1 then 실행명령;
                  when 값2 then 실행명령;
                  ...
                end;
*/
 
set serveroutput on;
 
-- if문 Ex)
declare
  emp_id employees.employee_id%type;
  emp_name employees.last_name%type;
  emp_dept employees.department_id%type;
  dept_name varchar2(20) := null;
begin
  select employee_id, last_name, department_id
  into emp_id,emp_name,emp_dept
  from employees
  where employee_id = 124;
   
  if(emp_dept = 50) then --if문 시작
    dept_name := 'Shipping';
  end if;
  if(emp_dept = 60) then
    dept_name := 'IT';
  end if;
  if(emp_dept = 70) then
    dept_name := 'Public Relation';
  end if;
   
  DBMS_OUTPUT.PUT_LINE(emp_id||' '||emp_name||' '||emp_dept||' '||dept_name);
end;
/
 
 
declare
  emp_id employees.employee_id%type;
  emp_name employees.last_name%type;
  emp_dept employees.department_id%type;
  dept_name varchar2(20) := null;
begin
  select employee_id, last_name, department_id
  into emp_id,emp_name,emp_dept
  from employees
  where employee_id = 103;
   
  if(emp_dept = 50) then
      dept_name := 'Shipping';
    elsif(emp_dept = 60) then -- elseif가 아니라 elsif임을 주의...
      dept_name := 'IT';
    elsif(emp_dept = 70) then
      dept_name := 'Public Relation';
    ELSE
      dept_name := 'Other';
  end if;
  DBMS_OUTPUT.PUT_LINE(emp_id||' '||emp_name||' '||emp_dept||' '||dept_name);
end;
/
 
 
declare
  emp_id employees.employee_id%type;
  emp_name employees.last_name%type;
  emp_comm employees.commission_pct%type := null;
begin
  select employee_id, last_name, commission_pct
  into emp_id, emp_name, emp_comm
  from employees
  where employee_id = 130;
   
  if (emp_comm > 0) then
    dbms_output.put_line(emp_id||' 의 보너스는 '||emp_comm);
  else
    DBMS_OUTPUT.PUT_LINE(emp_id||'의 보너스는 없습니다.');
  end if;
end;
/
 
 
-- case문 EX)
 
declare
  emp_id employees.employee_id%type;
  emp_name employees.last_name%type;
  emp_dept employees.department_id%type;
  dept_name varchar2(20) := null;
begin
  select employee_id, last_name, department_id
  into emp_id, emp_name, emp_dept
  from employees
  where employee_id = &empno; --치환변수 이용해 사용자로부터 입력받음
   
  dept_name := case emp_dept
                 when 50 then 'Shipping'
                 when 60 then 'IT'
                 when 70 then 'Public Relation'
                 when 80 then 'Sales'
               end;
  DBMS_OUTPUT.PUT_LINE(dept_name);
end;
/


[    ORACLE 날짜함수, 변환함수, decode(), case()  ]  





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
--[ ** 날짜 함수 ** ]
 
--sysdate : 현재 시스템 날짜를 구해옴
select sysdate AS 현재날짜 from dual;
 
--두 날짜 사이의 개월수를 출력하는 함수  : months_between(date1,date2) 
select ENAME,months_between(sysdate,hiredate) as 근무개월수 from EMP;
 
--개월수를 더하는 메서드 : add_months()
select add_months(sysdate,4) from dual; -- 현재 날짜에서 4개월을 더해줌
 
--다가올 특정일 날짜 가져오기 : next_day
select next_day(sysdate,'일요일') from dual; -- 현재날짜 기준 다가올 일요일의 날짜
 
--해당달의 마지막 일 수 구하기 : last_day()
select LAST_DAY(SYSDATE) from dual;
 
--형식을 지정해 날짜를 문자열로 변환시킨다. : to_char()
select to_char(sysdate,'yyyy-MM-dd') from dual;
 
--문자열을 날짜형으로 바까주는 메서드 : to_DATE()
select to_DATE('2017/10/27','yyyy/mm/dd') from dual;
 
--NULL일 경우 다른 값으로 치환하는 메서드(중요!! NULL과 산술연산시 결과는 무조건 NULL이기 때문에 적당한 처리 필요)   : NVL
select (NVL(sal,0) * 0.09) 세금 from EMP; -- sal값이 NULL인경우 0으로 계산되도록...
 
--switch문과 같은 역할을 하는 sql 메서드 : decode()
    -- deptno가 20일경우 '리서치부서'로 30일경우 '영업부'로 40일경우 '운영부'로 출력하는 코드
select DEPTNO, decode(deptno,20,'리서치부서',30,'영업부',40,'운영부') from DEPT;
 
--elseif문과 유사 : case()
select ENAME,DEPTNO,
  case when DEPTNO = 20 then '리서치부'
       when DEPTNO = 30 then '영업부'
       when DEPTNO = 40 then '운영부'
       else ' '
       end AS "부서명"
from EMP;


+ Recent posts