본문 바로가기

ORACLE/PL/SQL

명명 프로시저(Procedure) 기본 이론 & 생성 실습

명명 프로시저 기본


명명 프로시저

 - 특정 작업을 수행할수 있는 이름이 있는 PL/SQL 블록으로서, 매개 변수를 받을수 있고, 반복적으로 사용할수 있다.
 - 보통 연속 실행 또는 구현이 복잡한 트랜잭션을 수행하는 PL/SQL블록을 데이터베이스에 저장하기 위해 생성 한다.

 

 

만들어진 프로시져 목록 확인

select *

from user_procedures;

 

 

프로시저 소스 내용보기

select text
from user_source
where name = upper('emp_info');

 

 

프로시저 컴파일시 오류 발생 - 오류 내용보기

SQL> @c:\oracle\emp_info

경고: 컴파일 오류와 함께 프로시저가 생성되었습니다.

 

SQL> show errors


PROCEDURE EMP_INFO에 대한 오류:

LINE/COL ERROR
-------- -------------------------------------------------
12/2     PL/SQL: SQL Statement ignored
12/22    PL/SQL: ORA-00904: "LSAT_NAME": 부적합한 식별자
19/2     PL/SQL: Statement ignored
19/45    PLS-00201: 'V_SALARY' 식별자가 정의되어야 합니다

 

 

저장된 프로시저 실행

SQL> set serverouput on

 ->  DBMS_OUTPUT출력을 사용하기 위해 입력!

 

SQL> EXECUTE 프로시저이름(치환변수);

 

 

 

 

 

 

 

 

명명 프로시저 기본 실습


 

명명 프로시저 생성

--명명 Procedure

create or replace procedure emp_info

(p_employee_id in employees.employee_id%type)

 

is

             v_employee_id       employees.employee_id%type;

             v_last_name           employees.last_name%type;

             v_salary                employees.salary%type;

 

begin

             DBMS_OUTPUT.ENABLE;

--DBMS_OUTPUT을 사용하기 위해 선언.

 

             select employee_id, last_name, salary

             into v_employee_id, v_last_name, v_salary

             from employees

             where employee_id = p_employee_id;

            

             DBMS_OUTPUT.PUT_LINE( '사원번호 : ' || v_employee_id );

             DBMS_OUTPUT.PUT_LINE( '사원이름 : ' || v_last_name );

             DBMS_OUTPUT.PUT_LINE( '사원급여 : ' || v_salary );

 

end;

/

 

 

 

명명 프로시저 Table

--명명 Procedure

--PL/SQL Table(테이블)

create or replace procedure Table_Test

(v_deptno in emp.deptno%type)

 

is

    type empno_table is table of emp.empno%type

    index by binary_integer;

    type ename_table is table of emp.ename%type

    index by binary_integer;

    type sal_table is table of emp.sal%type

    index by binary_integer;

 

    empno_tab empno_table;

    ename_tab ename_table;

    sal_tab sal_table;

 

    i binary_integer :=0;

 

begin

    DBMS_OUTPUT.ENABLE;

 

    for emp_list in(select empno, ename, sal

            from emp

            where deptno = v_deptno) loop

           

            i := i+1;

 

        empno_tab(i) := emp_list.empno;

        ename_tab(i) := emp_list.ename;

        sal_tab(i) := emp_list.sal;

    end loop;

 

    for cnt in 1..i loop

                 DBMS_OUTPUT.PUT_LINE( '사원번호 : ' || empno_tab(cnt));

                 DBMS_OUTPUT.PUT_LINE( '사원이름 : ' || ename_tab(cnt));

                 DBMS_OUTPUT.PUT_LINE( '사원급여 : ' || sal_tab(cnt));

    end loop;

 

end;

/