zoukankan      html  css  js  c++  java
  • ORA-01403:no data found 解决办法(转载)

    1. 存储过程中 ORA-01403: no data found 错误

    在存储过程中,select 字段名  into  变量 from 表名 where .........;

    如果查询出来为空时, 会出现  ORA-01403: no data found 的错误

    command window:

    declare
    v_cnt number(10);
    begin
    select id into v_cnt from tt where 1=2;
    end;
    /

    错误:

    2. 解決办法:

    造成错误的原因主要是数据库中没有对应的数据。而当直接使用该查询语句时,是不会报错的,返回0记录。

    2.1. 对查询字段使用聚合函数

     command window:

    declare
    v_cnt number(10);
    begin 
    select max(id) into v_cnt from tt where 1=2;
    end; 

    select count(*) field into var from table where ....

    增加一个count(*)即使没有找到数据,也会返回0,而不是null。

    增加一个min或count(*)函数。这主要是因为聚合函数没有找到数据时,会返回0,而不是null。

    这些主要是聚合类型的函数,如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是返回空记录。

    2.2:捕获异常,用exception when no_data_found then  处理一下。

     declare
    v_cnt number(10);
    begin 
    select id into v_cnt from tt where 1=2;
    exception when no_data_found then
    raise_application_error(-20012,'error');
    --dbms_output.put_line('error1');
    end; 
    /

    https://blog.csdn.net/u014112828/article/details/50578385

  • 相关阅读:
    爬取B站up主相册原图
    爬MEIZITU网站上的图片
    mpvue
    修改Tomcat控制台标题
    iserver频繁崩溃、内存溢出事故解决小记
    Java反射机制详解 及 Method.invoke解释
    window下maven的环境搭建
    window下mongodb的安装和环境搭建
    centos7 安装 redis4.0.8
    centos7 安装mysql5.7.20(yum方式)
  • 原文地址:https://www.cnblogs.com/sannyhome/p/9293188.html
Copyright © 2011-2022 走看看