执行这个过程
SQL> exec procexception('a');
报错
ORA-01403: no data found
ORA-06512: at "LIFEMAN.PROCEXCEPTION", line 6
ORA-06512: at line 1
处理这个有三个办法
1. 直接加上异常处理。
create or replace procedure procexception(p varchar2)
as
v_postype varchar2(20);
begin
select pos_type into v_postype from pos_type_tbl where 1=0;
dbms_output.put_line(v_postype);
exception
when no_data_found then
dbms_output.put_line('没找到数据');
end;
、这样做换汤不换药,程序仍然被中断。可能这样不是我们所想要的。
2. select into做为一个独立的块,在这个块中进行异常处理
create or replace procedure procexception(p varchar2)
as
v_postype varchar2(20);
begin
begin
select pos_type into v_postype from pos_type_tbl where 1=0;
dbms_output.put_line(v_postype);
exception
when no_data_found then
v_postype := '';
end;
dbms_output.put_line(v_postype);
end;
这是一种比较好的处理方式了。不会因为这个异常而引起程序中断。
3.使用游标
create or replace procedure procexception(p varchar2)
as
v_postype varchar2(20);
cursor c_postype is select pos_type from pos_type_tbl where 1=0;
begin
open c_postype;
fetch c_postype into v_postype;
close c_postype;
dbms_output.put_line(v_postype);
end; 这样就完全的避免了no_data_found异常。完全交由程序员来进行控制了。
第二种情况是too_many_rows 异常的问题。
Too_many_rows 这个问题比起no_data_found要复杂一些。
给一个变量赋值时,但是查询结果有多个记录。
处理这种问题也有两种情况:
1. 多条数据是可以接受的,也就是说从结果集中随便取一个值就行。这种情况应该很极端了吧,如果出现这种情况,也说明了程序的严谨性存在问题。
对于第一种情况,就必须采用游标来处理,而对于第二种情况就必须使用内部块来处理,重新抛出异常。
多条数据可以接受,随便取一条,这个跟no_data_found的处理方式一样,使用游标。
我这里仅说第二种情况,不可接受多条数据,但是不要忘了处理no_data_found哦。这就不能使用游标了,必须使用内部块。
create or replace procedure procexception2(p varchar2)
as
v_postype varchar2(20);
begin
begin
select pos_type into v_postype from pos_type_tbl where rownum < 5;
exception
when no_data_found then
v_postype :=null;
when too_many_rows then
raise_application_error(-20000,'对v_postype赋值时,找到多条数据');
end;
dbms_output.put_line(v_postype);
end; 需要注意的是一定要加上对no_data_found的处理,对出现多条记录的情况则继续抛出异常,让上一层来处理。
总之对于select into的语句需要注意这两种情况了。需要妥当处理啊。
3.5 在存储过程中返回结果集
我们使用存储过程都是返回值都是单一的,有时我们需要从过程中返回一个集合。即多条数据。这有几种解决方案。比较简单的做法是写临时表,但是这种做法不灵活。而且维护麻烦。我们可以使用嵌套表来实现.没有一个集合类型能够与java的jdbc类型匹配。这就是对象与关系数据库的阻抗吧。数据库的对象并不能够完全转换为编程语言的对象,还必须使用关系数据库的处理方式。
create or replace package procpkg is
type refcursor is ref cursor;
procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor);
end procpkg;
create or replace package body procpkg is
procedure procrefcursor(p varchar2, p_ref_postypeList out refcursor)
is
v_posTypeList PosTypeTable;
begin
v_posTypeList :=PosTypeTable();--初始化嵌套表
v_posTypeList.extend;
v_posTypeList(1) := PosType('A001','客户资料变更');
v_posTypeList.extend;
v_posTypeList(2) := PosType('A002','团体资料变更');
v_posTypeList.extend;
v_posTypeList(3) := PosType('A003','受益人变更');
v_posTypeList.extend;
v_posTypeList(4) := PosType('A004','续期交费方式变更');
open p_ref_postypeList for select * from table(cast (v_posTypeList as PosTypeTable));
end;
end procpkg;
在包头中定义了一个游标变量,并把它作为存储过程的参数类型。
在存储过程中定义了一个嵌套表变量,对数据写进嵌套表中,然后把嵌套表进行类型转换为table,游标变量从这个嵌套表中进行查询。外部程序调用这个游标。
所以这个过程需要定义两个类型。
create or replace type PosType as Object ( posType varchar2(20), description varchar2(50) );
需要注意,这两个类型不能定义在包头中,必须单独定义,这样java层才能使用。
在外部通过pl/sql来调用这个过程非常简单。
set serveroutput on; declare type refcursor is ref cursor; v_ref_postype refcursor; v_postype varchar2(20); v_desc varchar2(50); begin