-
oracle PS/SQL_제어문DataBase/oracle 2020. 3. 28. 00:20
1. 조건문
1) if문
if(조건) then
실행명령;
end if;
1234567891011121314151617181920212223242526declareemp_id employees.employee_id%type;emp_name employees.last_name%type;emp_dept employees.department_id%type;dept_name varchar2(20);beginselect employee_id, last_name,department_idinto emp_id, emp_name, emp_deptfrom employeeswhere 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 Scripterif (조건) then
실행명령;
elsif(조건) then (elseif 아니라 elsif)
실행명령
end if
1234567891011121314151617181920212223declareemp_id employees.employee_id%type;emp_name employees.last_name%type;emp_dept employees.department_id%type;dept_name varchar2(20);beginselect employee_id, last_name,department_idinto emp_id, emp_name, emp_deptfrom employeeswhere 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 Scripterif (조건) then
실행명령;
else
실행명령
end if;
12345678910111213141516171819declareemp_id employees.employee_id%type;emp_name employees.last_name%type;emp_comm employees.COMMISSION_PCT%type := null; -- 선언하면서 초기화beginselect employee_id, last_name, commission_pctinto emp_id, emp_name, emp_commfrom employeeswhere employee_id = 155;-- 이때는 그냥 elseif (emp_comm > 0) thendbms_output.put_line(emp_name || '의 보너스는 '|| emp_comm);elsedbms_output.put_line(emp_name || '의 보너스는 없습니다');end if;end;/http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter2) case문
case 제어대상
when 조건 then 실행명령
when 조건 then 실행명령
end;
12345678910111213141516171819202122declareemp_id employees.employee_id%type;emp_name employees.last_name%type;emp_dept employees.department_id%type;dept_name varchar2(20);beginselect employee_id, last_name, department_idinto emp_id, emp_name, emp_deptfrom employeeswhere employee_id = &empno;dept_name := case emp_deptwhen 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 Scripter2. 반복문
1) basic loop문
loop
pl/sql문장 ;
exit 조건 ;
end loop;
** 조건에 해당되면 빠져나가겠다 / 조건이 만족안되면 pl/sql 다시 실행(만족할 때 까지)
** 조건을 나중에 검사. 일단 loop실행
12345678910111213declarenum number :=0;beginloopdbms_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 Scripter1234567891011declarenum number := 10;beginloopdbms_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 Scripter2) while문
while 조건 loop
실행문장 ;
end loop ;
** 조건을 먼저 검사
1234567891011declarenum number:=0;beginwhile num <11 loopdbms_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 Scripter1234567891011declarenum number := 10;beginwhile num >=0 loopdbms_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 Scripter3) for문
FOR i IN start..end LOOP
실행문장 ;
END LOOP ;
1234567891011121314beginFOR n IN 0..10 loopdbms_output.put_line( n );end loop;end;/-- 역순beginfor n in reverse 0..10 loopdbms_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
1234567891011121314151617181920212223declarecnt number;no number := &employee_id;sign varchar2(20);realname varchar2(20);beginselect length(first_name ), first_name into cnt, realnamefrom employeeswhere employee_id = no;sign := '';for n in 1..cnt loopsign := 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 : 보조제어문
123456789101112131415161718declaretot number :=0;beginfor i in 1..10 looptot := 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'DataBase > oracle' 카테고리의 다른 글
oracle PL/SQL_예외처리(Exception) (0) 2020.04.19 oracle PL/SQL_ 커서(cursor) (0) 2020.03.29 oracle PL/SQL_tableType 변수 (0) 2020.03.23 oracle PL/SQl_rowType변수 및 복합변수 활용 (0) 2020.03.22 oracle PL/SQL_변수 선언 및 데이터 타입 (0) 2020.03.22