ORA-01403:未找到用于选择进入的数据

mhd8tkvw  于 2022-10-04  发布在  Oracle
关注(0)|答案(5)|浏览(373)

我收到以下查询的ORA-01403: no data found异常。这一错误的可能性是什么?

SELECT trim(name)
  INTO fullname
  FROM ( SELECT n.name
         FROM directory dir, store n
        WHERE dir.name = n.name
          AND dir.status NOT IN ('F', 'L', 'M')
        ORDER BY n.imp, dir.date)
  WHERE rownum <= 1;

我如何处理此错误?

vpfxa7rd

vpfxa7rd1#

尽管您已经设置了WHERE条件,但更好的方法是处理未找到记录或‘未找到数据’错误。我会用SELECT语句自己的BEGIN/EXCEPTION/END块 Package SELECT语句来编写上面的代码。

代码可能如下所示:

BEGIN
    SELECT trim(name) 
    INTO fullName
    FROM (
        SELECT n.name
        FROM directory dir, store n
        WHERE dir.name = n.name
        AND dir.STATUS NOT IN ('F','L','M')           
        ORDER BY n.imp, dir.date
    )
    WHERE rownum <= 1;
EXCEPTION
    WHEN NO_DATA_FOUND THEN
        fullName := NULL;
END;
ukdjmx9f

ukdjmx9f2#

如果SanDeep描述的标准异常处理似乎开销很大(就像我的例子一样),而您使用NULL或某个个人*<未找到>*值就可以了,您可以这样转换它:

select  col  into  v_foo  from  bar  where 1=0  -- would provoke ORA-01403

=>否ORA-01403已提出:

-- if NULL would be fine:

select  (select  col  from  bar  where 1=0)  into  v_foo  from dual

-- if individual "NOT_FOUND" value should be set to avoid standard exception handling:
--    (it depends on your col type, so it could e.g. be 'NOT_FOUND' or -1 
--     or to_date( 'yyyy-mm-dd', '2100-01-01') )

select  nvl(  (select  col  from  bar  where 1=0),  'NOT_FOUND'  )  into  v_foo  from dual
rggaifut

rggaifut3#

可能是因为您的查询

SELECT n.name
        FROM directory dir,
          store n
        WHERE dir.name            = n.name
        AND dir.STATUS NOT                IN ('F','L','M')           
        ORDER BY n.imp,
          dir.date

不返回任何行

oug3syen

oug3syen4#

如果dir.status在您的表中有任何空值,则not in可能没有执行您认为的操作。即使您认为应该获得一行或多行,也可能会得到零行。您可以切换到and not (dir.status in ('F,'L','M')),而不是dir.status not in ('F','L','M')

参考this

mfuanj7w

mfuanj7w5#

忘掉例外吧..。只需使用DUAL表即可:

select (
SELECT trim(name)
  FROM ( SELECT n.name
         FROM directory dir, store n
        WHERE dir.name = n.name
          AND dir.status NOT IN ('F', 'L', 'M')
        ORDER BY n.imp, dir.date)
  WHERE rownum <= 1
) into fullname
from dual;

B-)这样,如果您的查询没有返回任何内容,变量将用NULL填充。

相关问题