SQL> declare
2 num number;
3 begin
4 select sum(num) into num
5 from s_test where c='cb';
6 dbms_output.put_line(num);
7 exception
when no_data_found then
8 dbms_output.put_line(100000);
9 end ;
10 /
PL/SQL procedure successfully completed
上面这段代码执行后,
因为没有相应的记录,
所以照理来说应该是进入异常处理部分的,
也就是‘dbms_output.put_line(100000);'。
之前已经运行过'set serverout on;'
问题:
为什么没有print 那个100000呢?
2 num number;
3 begin
4 select sum(num) into num
5 from s_test where c='cb';
6 dbms_output.put_line(num);
7 exception
when no_data_found then
8 dbms_output.put_line(100000);
9 end ;
10 /
PL/SQL procedure successfully completed
上面这段代码执行后,
因为没有相应的记录,
所以照理来说应该是进入异常处理部分的,
也就是‘dbms_output.put_line(100000);'。
之前已经运行过'set serverout on;'
问题:
为什么没有print 那个100000呢?
2 c varchar2(2);
3 begin
4 select * into c
5 from dual where 1=2;
6 dbms_output.put_line(c);
7 exception
8 when no_data_found then
9 dbms_output.put_line(100000);
10 end ;
11 /PL/SQL 过程已成功完成。SQL> set serverout on
SQL> declare
2 c varchar2(2);
3 begin
4 select * into c
5 from dual where 1=2;
6 dbms_output.put_line(c);
7 exception
8 when no_data_found then
9 dbms_output.put_line(100000);
10 end ;
11 /
100000PL/SQL 过程已成功完成。SQL>经测试,效果是一样,迷糊了。PL/SQL 过程已成功完成。
只不过,如果没有满足条件的记录,sum结果为空而已,
因此,该语句永远也不会产生异常,当然也就不执行异常块了!
SELECT COUNT(*) FROM (SELECT SUM(num) INTO num FROM s_test WHERE c = 'cb')
--试试这个就知道了
SELECT COUNT(*) FROM (SELECT SUM(num) INTO num FROM s_test WHERE c = 'cb')
SQL>
SQL> declare
2 num number;
3 begin
4 select * into num
5 from dual where 1=2;
6 exception
7 when no_data_found then
8 dbms_output.put_line(100000);
9 end ;
10 /100000PL/SQL procedure successfully completedSQL>
declare
num number;
begin
select null into num
from dual ;
dbms_output.put_line(num);
exception
when no_data_found then
dbms_output.put_line(100000);
end ;
declare
num number;
begin
select id into num
from ta ;
dbms_output.put_line(num);
exception
when no_data_found then
dbms_output.put_line(100000);
end ; --只有查询出来的空表,没有记录时,才会进入异常代码中