对于这个,找不到数据他直接就异常了,表面上看好像也没问题,走异常也能理解,以下是我的测试存过
create or replace procedure p_test_exception(i_prm varchar2)
as
v_username varchar2(10);
begin
select a.uname into v_username from t_test_staff1 a
where a.staffid=i_prm
;
if SQL%NOTFOUND then
insert into t_test_staff1(staffid) values (i_prm);
end if;
commit;
end ;
需要采用捕获no_data_found
异常的方式
exception
when no_data_found
你这个是另一个处理方式了,我那个可以不走异常就执行下去的,我只是不清楚为啥我那个不走sql%
@海月孤影: %FOUND
只对 INSERT, UPDATE, DELETE,SELECT INTO
有效,对 SELECT
无效
%NOTFOUND
This attribute is the logical opposite of %FOUND. It yields TRUE if an INSERT, UPDATE, or DELETE statement affected no rows, or a SELECT INTO statement returned no rows. Otherwise, it yields FALSE.
@dudu: 好吧,被游标误导了,结果误打误撞在游标写更新语句了
查了他的用法,看到有些网友提到了游标,我猜想应该是要在游标环境下才会走它,然后改了试试,然后真走了,下面是修改后的存过
create or replace procedure p_test_exception(i_prm varchar2)
as
v_username varchar2(10);
v_uname varchar2(10);
v_staffid varchar2(10);
CURSOR c_staff IS
SELECT a.staffid,a.uname
FROM t_test_staff1 a
;
begin
OPEN c_staff;
LOOP
FETCH c_staff INTO
v_staffid,v_uname;
EXIT WHEN c_staff%NOTFOUND;
update t_test_staff1 a
set a.uname=333333
where a.staffid=i_prm
;
if SQL%NOTFOUND then
insert into t_test_staff1(staffid) values (i_prm);
end if;
commit;
end loop;
end ;