본문 바로가기

Database/PLSQL

(109)
029 - [Oracle PL/SQL] Exceptions - User defined Error declare v_employee_id number:=1; begin update employees set salary=20000 where employee_id=v_employee_id; dbms_output.put_line(sql%rowcount); end; -------------------------------------- # 조건에 맞는 데이터가 없으므로 업데이트 되는 데이터는 없다. # 그래서 적용된 row 개수는 0 이다. 0 # 에러는 아니지만 조건에 맞는 데이터가 없는 경우 에러를 발생시키는 샘플 declare v_employee_id number:=1; e_invalid_no exception; begin update employees set salary=20000 where emplo..
028 - [Oracle PL/SQL] Exceptions - Non predefined error # 오류 상황을 만들어서 해당 오류 코드를 확인한다. begin insert into departments(DEPARTMENT_ID ,DEPARTMENT_NAME) values (1,null ); end; ---------------------------------------------- ORA-01400: cannot insert NULL into ("HR"."DEPARTMENTS"."DEPARTMENT_NAME") ORA-06512: at line 2 01400. 00000 - "cannot insert NULL into (%s)" *Cause: An attempt was made to insert NULL into previously listed objects. *Action: These objec..
027 - [Oracle PL/SQL] Exceptions - common mistakes 샘플코드에서 사용한 데이터는 HR 스키마이고, 오라클 설치시 생성할 수 있는 기본 스키마 입니다. Exceptions 사용시 흔한 실수 1. for 같은 반복문과 예외처리 할 경우, 예외처리후에 반복문을 계속 처리해야 한다면 아래 코드같은 구조를 조심해야 한다. # 반복할 for 문이 있음에도 불구하고 예외처리 후 구문이 종료되는 잘못된 경우의 예시 코드. declare v_first_name employees.first_name%type; begin for i in 99..102 loop select first_name into v_first_name from employees where employee_id=i; dbms_output.put_line(i||' ' ||v_first_name); end ..
026 - [Oracle PL/SQL] Exceptions PL/SQL Basic structure Handling Exceptions with PL/SQL # no exception case. declare v_first_name employees.first_name%type; begin select first_name into v_first_name from employees where employee_id=1; -- there is no emp_id=1 end; ------------------------------------------------- Error report - ORA-01403: no data found ORA-06512: at line 5 01403. 00000 - "no data found" *Cause: No data was found..
025 - [Oracle PL/SQL] Cursor - FOR UPDATE & CURRENT OF select * from employees where employee_id in (100,200) order by 1 for update; # 현재 상황에서는 employees 테이블은 잠김상태가 되므로 다른 업데이트 및 삭제 등, # 수정을 할 수 없다. commit; # commit 명령어가 실행될때까지 변경할 수 없다. for update 실행화면 for update 때문에 다른 세션에서 update 문이 완료되지 않는다 commit 실행 다른 세션에서 update 문이 완료된다 롤백을 통해서 원래대로 복구 DECLARE CURSOR c_emp_dept30 is SELECT employee_id, first_name,salary FROM employees where department_id=30 f..
024 - [Oracle PL/SQL] Cursor - Cursor with Parameters DECLARE CURSOR c_emp_dept(v_dept number) --here we defined the parameter without size is SELECT employee_id, first_name FROM employees where department_id=v_dept; v_empno employees.employee_id%type; v_first_name employees.first_name%type; BEGIN OPEN c_emp_dept(10); dbms_output.put_line('dept 10 contains:'); loop fetch c_emp_dept into v_empno, v_first_name; exit when c_emp_dept%notfound; dbms_out..
023 - [Oracle PL/SQL] Cursor - For loop cursor # basic loop cursor DECLARE CURSOR c_emp_dept30 is SELECT employee_id, first_name FROM employees where department_id=30 order by employee_id; v_empno employees.employee_id%type; v_first_name employees.first_name%type; BEGIN OPEN c_emp_dept30; loop fetch c_emp_dept30 into v_empno, v_first_name; exit when c_emp_dept30%notfound; dbms_output.put_line(v_empno||' '||v_first_name); end loop; close c_em..
022 - [Oracle PL/SQL] Cursor - Explicit Cursor Attributes # using c_emp%notfound, c_emp%isopen,c_emp%rowcount DECLARE CURSOR c_emp is SELECT employee_id,first_name FROM employees order by employee_id; v_empno employees.employee_id%type; v_first_name employees.first_name%type; BEGIN if c_emp%isopen then null; else open c_emp; end if; dbms_output.put_line('the counter for cursor now is '||c_emp%rowcount); loop fetch c_emp into v_empno, v_first_name; exit..