본문 바로가기

전체 글

(646)
061 - [Oracle PL/SQL] Dynamic SQL - INTO Clause 간단한 into 샘플 코드 # dynamic sql with single row query # 익명블록, into 절을 사용한 경우 declare v_ename varchar2(100); begin execute immediate 'select first_name from employees where employee_id=100' into v_ename; DBMS_OUTPUT.put_line(v_ename); end; # 익명블록, into/using 절을 동시에 사용한 경우 # into 부분이 using 앞에 위치해야 한다 declare v_ename varchar2(100); vno number:=200; begin execute immediate 'select first_name from emplo..
060 - [Oracle PL/SQL] Dynamic SQL - USING Clause # EXECUTE IMMEDIATE with USING # 우선 테이블 데이터를 모두 삭제한다 delete from emp1; select * from emp1; -- this procedure can work with any table contain 1 coulmn and -- this column should be number create or replace procedure add_rows ( p_table_name varchar2, p_value number ) is begin --EXECUTE IMMEDIATE 'insert into '||p_table_name ||' values('||p_value||') '; EXECUTE IMMEDIATE 'insert into '||p_table_name..
059 - [Oracle PL/SQL] Dynamic SQL 테스트 환경 준비 # 새로운 테이블을 생성 drop table emp1; create table emp1 ( emp_id number ); drop table emp2; create table emp2 ( emp_id number ); # 데이터 입력 begin insert into emp1 values (1); insert into emp1 values (2); insert into emp1 values (3); insert into emp2 values (1); insert into emp2 values (2); insert into emp2 values (3); commit; end; # 입력결과 확인 select * from emp1; select * from emp2; --------------..
058 - [Oracle PL/SQL] Oracle package - UTL_FILE 사전에 필요한 작업들, 서버 환경에 따라서 폴더 옵션은 다름 to read/write file , we need to create directory create directory only for sys and system user 1- open sqlplus 2- conn as sysdba 3- alter session set container=orclpdb ( the plug db name ) 4- create directory mydir as '/home/oracle' 5- grant READ, WRITE on DIRECTORY MYDIR to public; 6- CREATE THE DIRECTORY ON your computer ( the server ) 7- put empty file sample..
057 - [Oracle PL/SQL] Oracle package - DBMS_OUTPUT •PUT appends text from the procedure to the current line of the line output buffer. •NEW_LINE places an end-of-line marker in the output buffer. •PUT_LINE combines the action of PUT and NEW_LINE (to trim leading spaces). •GET_LINE retrieves the current line from the buffer into a procedure variable. •GET_LINES retrieves an array of lines into a procedure-array variable. •ENABLE/DISABLE enables a..
056 - [Oracle PL/SQL] Package - index by tables in packages 아래 샘플은 index by table을 이용한 커서 결과를 저장하는 코드입니다. create or replace package emp_pkg is type emp_table_type is table of employees%rowtype index by binary_integer; procedure get_employees(p_emps out emp_table_type ); end; / create or replace package body emp_pkg is procedure get_employees(p_emps out emp_table_type ) is begin for emp_record in (select * from employees) loop p_emps(emp_record.employee_i..
055 - [Oracle PL/SQL] Package - Persistent State and cursor 커서를 종료하지 않으면 현재 상태를 계속유지하고 있기 때문에 이전에 실행한 특정 상황이후 부터 다시 시작할 수 있다. 아래 샘플 코드를 보면 이해가 쉽다. 패키지를 내부의 프로시져에서 패치를 20개씩만 할 경우, 이전에 실행한 위치를 기억하여 다음 실행위치부터 자동으로 실행된다. create or replace package cur_pkg is cursor c_emp is select employee_id from employees; procedure open; procedure close; procedure printnext_20; end; / create or replace package body cur_pkg is procedure open is begin if not c_emp%isopen the..
054 - [Oracle PL/SQL] Package - Persistent State 해당 세션이 유지되는 동안, 패키지 내부의 변수가 값을 기억하고 있는 것을 의미한다. 아래 샘플코드는 패키지 내부에 변수를 선언하고 해당 변수에 입력받은 값을 저장하는 패키지 이다. create or replace package Persistent_state is g_var number:=10; procedure update_g_var ( p_no number); end; / create or replace package body Persistent_state is procedure update_g_var ( p_no number) is begin g_var:=p_no; dbms_output.put_line(g_var); end; end; 패키지 내부의 변수 g_var 에 저장된 값이 세션이 유지되는 동..