본문 바로가기

분류 전체보기

(667)
067 - [Oracle PL/SQL] AUTONOMOUS TRANSACTION 블록간 구분이 없어서 subprogram 에서 실행한 commit 이 main procedure 에 영향을 미치는 샘플 코드 #case one without using PRAGMA AUTONOMOUS_TRANSACTION drop table t; # hr 사용자 세션에서 실행 CREATE TABLE t (test_value VARCHAR2(25)); / CREATE OR REPLACE PROCEDURE child_block IS BEGIN INSERT INTO t(test_value) VALUES ('Child block insert'); COMMIT; END child_block; / CREATE OR REPLACE PROCEDURE parent_block IS BEGIN INSERT INTO t(te..
066 - [Oracle PL/SQL] Design - Definer's right, Invoker's rights Definer's rights 무슨 의미인지 샘플 코드를 통해서 확인 # 테스트를 위한 준비과정 # hr 사용자로 디비 접속해서 테이블 생성. create table hr_table ( id number, name varchar2(100) ); /* Table HR_TABLE created. */ create or replace procedure add_hr_table ( p_id number, p_name varchar2 ) is begin insert into hr_table values (p_id,p_name); end; /* Procedure ADD_HR_TABLE compiled */ # 여기부터는 sys 사용자로 접속하여 실행 --now conn as sys as sysdba and creat..
065 - [Oracle PL/SQL] Design - Standardizing 에러처리를 고급스럽게 처리하는 방법, 샘플코드, 사용자 정의 오류와 오라클 표준 에러를 동시에 처리하는 방법 delete from DEPARTMENTS /* Error report - ORA-02292: integrity constraint (HR.EMP_DEPT_FK) violated - child record found */ declare e_fk_err exception; pragma EXCEPTION_INIT (e_fk_err, -02292); begin delete from DEPARTMENTS; exception when e_fk_err then RAISE_APPLICATION_ERROR (-20001, 'error'); end; /* Error report - ORA-20001: error ..
064 - [Oracle PL/SQL] Dynamic SQL - DBMS_SQL Package VS Execute immediate 다이나믹 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 *..
063 - [Oracle PL/SQL] Dynamic SQL - to compile packages Execute immediate 를 이용한 다양한 샘플 코드 # we can also use dynamic sql to execute anonymous-block declare v_code varchar2(100):= 'begin dbms_output.put_line(''welcome''); end; '; begin execute immediate v_code; end; ---------------------------------------------- # procedure 컴파일 alter procedure ADD_ROWS compile; ---------------------------------------------- Procedure ADD_ROWS altered. # function 컴파일 al..
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; --------------..