프로시저 조건 질문좀 할게요. 아래와 같이 test1, test2, test3의 각각 테이블이 있는데
맨처음 test1에 test2 데이터를 insert한 후 test1의 a1컬럼의 중복값이 있가, 길이가 5자리 아닌값, null값이 있는가 체크 후에 test3에 test1의 데이터를 insert하고 싶습니다.
만약 test1의 a1컬럼에 저 3가지 조건값중에 하나라도 존재하면 바로 프로시저를 중단해야 해아는데 조건을 어떻게 줘야 할까요?
create table test1 (
no number ,
name varchar2(5),
a1 varchar2(5)
)
;
create table job_test2 (
no number ,
name varchar2(5),
a1 varchar2(5)
)
;
create table job_test3 (
no number ,
name varchar2(5),
a1 varchar2(5)
)
;
create or replace procedure pro_test01
is
begin
insert into test1
select * from test2;
commit;
select a1 from job_test2
group by a1
having count(*) > 1
;
select * from job_test2
where length(a1) <> '5'
;
select * from job_test2
where a1 is null
;
insert into test3
select * from test1;
commit;
END pro_test01
;
/
대략 exception 만 가게 하면 될것 같네요.
create or replace procedure pro_test01
IS
v_step VARCHAR2(150);
v_cnt NUMBER;
begin
insert into test1
select * from test2;
commit;
v_step := 'dup check';
SELECT COUNT(*)
INTO v_cnt
FROM (
select a1 from job_test2
group by a1
having count(*) > 1);
IF v_cnt > 0 THEN
RAISE program_error;
END IF;
v_step := 'length check';
select COUNT(*) INTO v_cnt from job_test2
where length(a1) <> '5'
;
IF v_cnt > 0 THEN
RAISE program_error;
END IF;
v_step := 'null check';
select COUNT(*) INTO v_cnt from job_test2
where a1 is null
;
IF v_cnt > 0 THEN
RAISE program_error;
END IF;
insert into test3
select * from test1;
commit;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(v_step ||' : error ');
END pro_test01