DataBase/oracle

oracle PS/SQL_제어문

HUCHUCHU 2020. 3. 28. 00:20

 

1. 조건문

  1) if문

     

        if(조건) then

            실행명령;

        end if;

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
declare
    emp_id employees.employee_id%type;
    emp_name employees.last_name%type;
    emp_dept employees.department_id%type;
    dept_name varchar2(20);
    
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';
    end if;
    if( emp_dept = 60 ) then dept_name :='IT';
    end if;
    if( emp_dept = 70 ) then dept_name :='P/L';
    end if;
    if( emp_dept = 80 ) then dept_name :='sales';
    end if;
    
    dbms_output.put_line(emp_id||' '|| emp_name ||' '|| emp_dept||' '|| dept_name);
    
end;
 
/
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

        if (조건) then

           실행명령;

        elsif(조건) then  (elseif 아니라 elsif)

           실행명령

        end if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
declare
    emp_id employees.employee_id%type;
    emp_name employees.last_name%type;
    emp_dept employees.department_id%type;
    dept_name varchar2(20);
    
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 dept_name :='IT';    --else if 가 아니다!!elsif다 와웅
    elsif( emp_dept = 70 ) then dept_name :='P/L';
    elsif( emp_dept = 80 ) then dept_name :='sales';
    end if;
    
    dbms_output.put_line(emp_id||' '|| emp_name ||' '|| emp_dept||' '|| dept_name);
end;
 
/
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

        if (조건) then 

           실행명령;

        else

           실행명령

        end if;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 = 155;
    
    -- 이때는 그냥 else
    if (emp_comm > 0) then 
    dbms_output.put_line(emp_name || '의 보너스는 '|| emp_comm);
    else
    dbms_output.put_line(emp_name || '의 보너스는 없습니다');
    end if;
end;
 
/
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

   2) case문

 

        case 제어대상

           when 조건 then 실행명령

           when 조건 then 실행명령

        end;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
declare
    emp_id employees.employee_id%type;
    emp_name employees.last_name%type;
    emp_dept employees.department_id%type;
    dept_name varchar2(20);
    
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 'P/L'
                            when 80 then 'sales'
                            end;
dbms_output.put_line(emp_id||' '|| emp_name ||' '|| emp_dept||' '|| dept_name);
end;
 
/
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

2. 반복문

 

   1) basic loop문

 

        loop

           pl/sql문장 ; 

           exit 조건 ; 

        end loop;

** 조건에 해당되면 빠져나가겠다 / 조건이 만족안되면 pl/sql 다시 실행(만족할 때 까지)

** 조건을 나중에 검사. 일단 loop실행

1
2
3
4
5
6
7
8
9
10
11
12
13
declare
    num number :=0;
 
 begin 
    loop
        dbms_output.put_line(num);
        num := num+1;
        exit when num > 10;
    end loop;
 
end;
 
/
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
1
2
3
4
5
6
7
8
9
10
11
declare
    num number := 10;
 
begin
    loop
        dbms_output.put_line(num);
        num := num-1;
        exit when num = -1;
    end loop;
end;
/
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

   2) while문

       

        while 조건 loop

           실행문장 ; 

        end loop ;        

** 조건을 먼저 검사

1
2
3
4
5
6
7
8
9
10
11
declare
    num number:=0;
    
begin
    while num <11 loop
        dbms_output.put_line(num);
        num := num+1;
    end loop;
end;
 
/
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
1
2
3
4
5
6
7
8
9
10
11
declare
    num number := 10;
 
begin
    while num >=0 loop
        dbms_output.put_line(num);
    num := num -1;
    end loop;
end;
/
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

   3) for문

 

       FOR i IN start..end LOOP

          실행문장 ;

       END LOOP ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
begin
    FOR n IN 0..10 loop
        dbms_output.put_line( n );    
    end loop;
end;
/
 
-- 역순
begin
    for n in reverse 0..10 loop
        dbms_output.put_line( n );    
    end loop;
end;
/
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

-- 사원테이블에서 사원 id를 입력받아서 사원 이름의 문자 수만큼 ##을 찍는 pl/sql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
declare
    cnt number; 
    no number := &employee_id;
    sign varchar2(20);
    realname varchar2(20);
begin
    select length(first_name ), first_name into cnt, realname 
    from employees
    where employee_id = no;
    
    sign := '';
    
    for n in 1..cnt loop
        sign := sign || '#';
    end loop;    
    
    dbms_output.put_line (realname);
    dbms_output.put_line (sign);
end;
 
/
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

** continue : 보조제어문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
declare
    tot number :=0;
begin
    for i in 1..10 loop
        tot := tot+1;
        dbms_output.put_line ('tot : ' || tot );
        
        continue when (i>5); -- i가 5이상이면 아래 실행문을 실행하지않고 위로 올라간다
        tot := tot+i;
        dbms_output.put_line ('tot2 : ' || tot );
        
 
    end loop;
 
end;
 
/
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter