본문 바로가기

plsql

(12)
044 - [Oracle PL/SQL] Package # create a function to calc square area create or replace function square_area ( p_side number ) return number is begin return p_side*p_side; end; select square_area(4) from dual; -------------------------------------------- SQUARE_AREA(4) -------------- 16 # create a function to calc rectangle area create or replace function rectangle_area ( p_l number,p_w number ) return number is begin return..
032 - [Oracle PL/SQL] Procedure - (IN parameter)/ Error 처리 # CLI 환경에서 컴파일하는 방법 # 세미콜론을 누락해서 오류가 발생한 상황과 오류를 조회하는 방법 SQL> CREATE OR REPLACE PROCEDURE UPDATE_SAL (P_EMP_ID IN NUMBER, P_AMOUNT IN NUMBER) IS --here you define variables --n number; BEGIN UPDATE employees set salary=salary+P_AMOUNT where employee_id=P_EMP_ID -- 세미콜른 삭제하고 컴파일해서 오류 확인하기 commit; exception WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE (SQLCODE); DBMS_OUTPUT.PUT_LINE (SQLERRM); END; 2 3 4..
031 - [Oracle PL/SQL] Procedure Anonymous Blocks 와 Subprograms 간의 차이점은 아래와 같다 다음과 같은 특징들이 있다. Oracle에서 익명 블록은 PL/SQL 문을 하나의 단위로 실행하는 것을 말합니다. 아래와 같은 장점이 있습니다: 캡슐화: 익명 블록을 사용하면 SQL 및 PL/SQL 문을 하나의 논리적인 단위로 캡슐화할 수 있습니다. 코드를 논리적으로 구성하고 정리하는 데 도움이 됩니다. 변수 선언 및 초기화: 익명 블록 내에서 변수를 선언하고 초기화할 수 있습니다. 이는 변수의 범위를 더 잘 제어하고 명명 충돌을 피하는 데 도움이 됩니다. 트랜잭션 제어: 익명 블록을 사용하여 COMMIT 및 ROLLBACK 문을 사용하여 트랜잭션을 명시적으로 제어할 수 있습니다. 일련의 작업의 원자성을 보장해야 하는 경우..
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..
013 - [Oracle PL/SQL] Records - %type What is a PL/SQL Record A PL/SQL record is a composite data structure that is a group of related data stored in fields. Each field in the PL/SQL record has its own name and data type. Declaring a PL/SQL Record 1- programmer-defined records. 2- table-based record. %Rowtype 3- cursor-based record. ( will be covered later ) # 아래 2가지 표현 방식은 동일한 방식이다. DECLARE TYPE t_EMP IS RECORD ( V_EMP_id employees..
012 - [Oracle PL/SQL] Continue Statement # i want to print under the number the Symbol :) only for 1,2,3,4,5 # there are many methods --method 1 declare v_sym varchar2(100); begin for i in 1..10 loop if i between 1 and 5 then v_sym:=i||chr(10)||':)'; else v_sym:=i; end if; dbms_output.put_line (v_sym); end loop; end; --------------------------------------- --method 2 begin for i in 1..10 loop dbms_output.put_line (i); continue when i>5..
011 - [Oracle PL/SQL] Nested Loops and Labels 이중 for 문에 대해서 알아보겠습니다. 일반적인 프로그래밍에서 사용하는 이중 for문과 유사합니다. declare v_star varchar2(100); begin for i in 1..5 loop for j in 1..i loop v_star:=v_star||'*'; end loop; dbms_output.put_line(v_star); v_star:=null; end loop; end; -------------------------------- * ** *** **** ***** # 위의 for문 샘플에서 이름을 지정한 샘플입니다. declare v_star varchar2(100); begin for i in 1..5 loop for j in 1..i loop v_star:=v_star||'*';..
010 - [Oracle PL/SQL] For Loop # basic for loop begin for i in 1..3 loop dbms_output.put_line('welcome '||i); end loop; end; ------------------------ begin for i in 1..1 loop dbms_output.put_line('welcome '||i); end loop; end; -------------------- begin for i in 3..5 loop dbms_output.put_line('welcome '||i); end loop; end; -------------------- welcome 1 welcome 2 welcome 3 welcome 1 welcome 3 welcome 4 welcome 5 # 감소하는 for 문 ..