ORA-01403:no data found 及 select a into b 空值
来源:互联网 发布:h3交换机 网络聚合命令 编辑:程序博客网 时间:2024/04/29 17:26
1. 存储过程中 ORA-01403: no data found 错误
在存储过程中,select 字段名 into 变量 from 表名 where .........;
如果查询出来为空时, 会出现 ORA-01403: no data found 的错误
测试表:
create table TEST_TABLE( T_ID NUMBER, T_NAME VARCHAR2(20)) ;
测试存储过程:
create or replace procedure pro_test isv_id test_table.t_id%type;begin select t_id into v_id from test_table where rownum = 1;end pro_test;
错误:
2. 解決办法:
造成错误的原因主要是数据库中没有对应的数据。而当直接使用该查询语句时,是不会报错的,返回0记录。
2.1. 对查询字段使用聚合函数
增加一个min函数。这主要是因为聚合函数没有找到数据时,会返回0,而不是null。
存储过程改为:
create or replace procedure pro_test isv_id test_table.t_id%type;begin select min(t_id) into v_id from test_table where rownum = 1;end pro_test;
这些主要是聚合类型的函数,如sum,count,max,min等。其的函数则不行,如to_char,substr.
另外,如使用nvl,即
select nvl(t_id,1) into v_id from test_table where rownum = 1;
是没效果的,还是会报异常。nvl只对null值处理,而select t_id into v_id from table是返回空记录。
缺点:1. 使用集合函数后可能偏离你所需要查找的值;2. 在数据量比较大时,这种方法明显会降低效率。
参考:oracle 中使用 select a into b 时遇到空值问题
2.2. 使用异常处理
即加入异常处理
create or replace procedure pro_test isv_id test_table.t_id%type;begin select t_id into v_id from test_table where rownum = 1; exception when no_data_found then -- no_data_found 也可以换成 others null; --其它操作。。。end pro_test;
但加入异常处理后,当抛出异常,程序无法继续下面的语句。即我们不能从异常句柄再重新回到当前块。这时可以增加begin...end 模拟TRY..CATCH..块
create or replace procedure pro_test is v_id test_table.t_id%type;begin begin select t_id into v_id from test_table where rownum = 1; exception when no_data_found then -- no_data_found 也可以换成 others null; --其它操作。。。 end; insert into test_table values (1, 'test');end pro_test;
另外还有一个问题就是,当多个地方抛出同一个异常时,如果判别是哪条语句抛出的异常呢?
可以使用定位变量标记异常发生点。
create or replace procedure pro_test is v_id test_table.t_id%type; stmt integer := 1;begin select t_id into v_id from test_table where rownum = 1; stmt := 2; select t_id into v_id from test_table where rownum = 1;exception when no_data_found then insert into errors values ('Error statment', stmt); end pro_test;参考:第七章 控制PL/SQL错误
2.3. 使用游标替代
create or replace procedure pro_test is v_id test_table.t_id%type; type cursor_type is ref cursor; cur cursor_type;begin open cur for select t_id from test_table where rownum = 1; fetch cur into v_id; close cur;end pro_test;
参考:ORA-01403:no data found exception的解决小道(转)
其他:Oracle中的Null值解析
- ORA-01403:no data found 及 select a into b 空值
- ORA-01403: no data found
- ORA-2330 ORA-01403: no data found
- 防止 ORA-01403: no data found
- ORA-01403:no data found 解决办法
- ORA-01403:no data found 解决办法
- ORA-01403:no data found 解决办法
- oracle 中使用 select a into b 时遇到空值问题
- Oracle 中使用 select a into b 时遇到空值问题(大坑)
- ora-1403 :no data found
- 触发器ORA-01403: no data found的解决方法
- select into 遇到空值
- Oracle 10.2 流复制问题(一)—— ORA-01403: no data found
- Mysql select data from one table into a new one
- Oracle error 1403: java.sql.SQLException: ORA-01403: no data found ORA-06512: at line 1 has been det
- oracle insert into A select from B 的一些心得
- ORA-01403 :not data found 处理方式
- select into 多个值 为空
- 【Struct(结构体)杂谈之三】借我一双慧眼吧,让我把C++中Class(类)和Struct(结构体)看个清清楚楚明明白白...
- ftp的port和pasv模式
- KMP字符串模式匹配详解
- table表格的制作
- Linux fork 函数详解
- ORA-01403:no data found 及 select a into b 空值
- (Standard Touch Delegate 和 Targeted Touch Delegate)CCLayer中的两种Touch类型
- 转]log4cxx以时间,大小的备份
- UI设计师必读的13篇文章
- java中readline方法会自动去除换行符
- [转载] 上海交通大学计算机系2013届研究生offer汇总
- STL_map用法
- 十元的耳塞到万元耳塞,之间到底有什么区别
- 99乘法