SQL> create table t2 ( cnt int );
Table created.
SQL> insert into t2 values ( 0 );
1 row created.
SQL> commit;
Commit complete.
SQL> create table t ( x int check ( x>0 ) );
Table created.
SQL> create trigger t_trigger
before insert or delete on t for each row
begin
if ( inserting ) then
update t2 set cnt = cnt + 1;
else
update t2 set cnt = cnt - 1;
end if;
dbms_output.put_line('I fired and updated '||sql%rowcount||' rows');
end;
/
Trigger created.
SQL> set serveroutput on
SQL> insert into t values (1);
I fired and updated 1 rows
1 row created.
SQL> insert into t values(-1);
I fired and updated 1 rows
insert into t values(-1)
*
ERROR at line 1:
ORA-02290: check constraint (LIM.SYS_C0021582) violated
SQL> select * from t2;
CNT
----------
1
SQL> rollback;
Rollback complete.
SQL> select * from t2;
CNT
----------
0
SQL> select * from t;
no rows selected
SQL>
SQL> create or replace procedure p as
2 begin
3 insert into t values(1);
4 insert into t values(-1);
5 end;
6 /
Procedure created.
SQL>
SQL> exec p;
I fired and updated 1 rows
I fired and updated 1 rows
begin
*
ERROR at line 1:
ORA-02290: check constraint (LIM.SYS_C0021582) violated
ORA-06512: at "LIM.P", line 4
ORA-06512: at line 2
SQL> select * from t2;
CNT
----------
0
SQL> select * from t;
no rows selected
SQL>
SQL> begin
2 p;
3 exception when others then
4 dbms_output.put_line('Error : '||sqlerrm);
5 end;
6 /
I fired and updated 1 rows
I fired and updated 1 rows
Error : ORA-02290: check constraint (LIM.SYS_C0021582) violated
PL/SQL procedure successfully completed.
SQL> select * from t;
X
----------
1
SQL> select * from t2;
CNT
----------
1
SQL>
-------- 프로시저 안에 예외처리를 해도 마찬가지 ---------
SQL> create or replace procedure p as
begin
insert into t values(1);
insert into t values(-1);
exception when others then
dbms_output.put_line('Error : '||sqlerrm);
end;
/
Procedure created.
SQL> exec p;
I fired and updated 1 rows
I fired and updated 1 rows
Error : ORA-02290: check constraint (LIM.SYS_C0021582) violated
PL/SQL procedure successfully completed.
SQL> select * from t;
X
----------
1
SQL> select * from t2;
CNT
----------
1
SQL>
- 강좌 URL : http://www.gurubee.net/lecture/4022
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.