<샘플코드에서 사용한 데이터는 HR 스키마이고, 오라클 설치시 생성할 수 있는 기본 스키마 입니다>
# 테스트용 테이블과 프로시져를 생성
--drop table products;
create table products
( prod_id number,
prod_name varchar2(20),
prod_type varchar2(20),
constraint products_pk primary key (prod_id)
);
----------------------------------------------------------------
Table PRODUCTS created.
----------------------------------------------------------------
create or replace procedure add_products
(p_prod_id number,p_prod_name varchar2,p_prod_type varchar2)
is
begin
insert into products values (p_prod_id,p_prod_name,p_prod_type);
commit;
exception
when others then
dbms_output.put_line ('error in insert ');
dbms_output.put_line (sqlcode);
dbms_output.put_line (sqlerrm);
end;
----------------------------------------------------------------
Procedure ADD_PRODUCTS compiled
# 1.positional - 변수의 입력 위치를 순서에 맞게 넣어야 한다.
execute add_products (1,'Laptop','SW');
select * from products;
PROD_ID PROD_NAME PROD_TYPE
---------- -------------------- --------------------
1 Laptop SW
--try to miss one parameter
-- it will give wrong number or types of arguments in call to 'ADD_PRODUCTS'
# 인자를 3개중에 2개만 입력하여 오류 발생.
execute add_products (2,'PC');
----------------------------------------------------------------
Error report -
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'ADD_PRODUCTS'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
--try to enter existing product
--this will go to exception
# primary key 중복으로 예외처리 발생
execute add_products (1,'Laptop','SW');
----------------------------------------------------------------
error in insert
-1
ORA-00001: unique constraint (HR.PRODUCTS_PK) violated
# 2.named - 인자이름과 값을 매칭시켜서 전달하는 방법, 이경우는 인자순서를 변경해도 문제가 없다.
execute add_products (p_prod_id=>2,p_prod_name=>'PC',p_prod_type=>'SW');
select * from products;
----------------------------------------------------------------
PROD_ID PROD_NAME PROD_TYPE
---------- -------------------- --------------------
1 Laptop SW
2 PC SW
execute add_products (p_prod_name=>'Keyboard',p_prod_id=>3,p_prod_type=>'HD');
select * from products;
----------------------------------------------------------------
PROD_ID PROD_NAME PROD_TYPE
---------- -------------------- --------------------
1 Laptop SW
2 PC SW
3 Keyboard HD
# 3.mixed - 인자의 순서를 맞추거나 변수명을 지정하여 값을 전달할수 있다.
execute add_products (4,p_prod_type=>'SW',p_prod_name=>'Windows 10');
select * from products;
----------------------------------------------------------------
PROD_ID PROD_NAME PROD_TYPE
---------- -------------------- --------------------
1 Laptop SW
2 PC SW
3 Keyboard HD
4 Windows 10 SW
'Database > PLSQL' 카테고리의 다른 글
037 - [Oracle PL/SQL] Procedure - Exception handling in multiple blocks (0) | 2024.02.23 |
---|---|
036 - [Oracle PL/SQL] Procedure - Default option for Parameters (0) | 2024.02.23 |
034 - [Oracle PL/SQL] Procedure - (IN OUT parameter) (0) | 2024.02.23 |
033 - [Oracle PL/SQL] Procedure - (OUT parameter) (0) | 2024.02.23 |
032 - [Oracle PL/SQL] Procedure - (IN parameter)/ Error 처리 (0) | 2024.02.23 |