我得到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;
Run Code Online (Sandbox Code Playgroud)
我该如何处理这个错误?
虽然你已经把条件放在哪里,但更好的方法是处理未找到记录的情况或"找不到数据"错误.我会在上面编写代码,用它自己的BEGIN/EXCEPTION/END块包装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;
Run Code Online (Sandbox Code Playgroud)
如果 Sandeep描述的标准异常处理似乎有很大的开销(就像在我的情况下那样)并且您对某个NULL或某个单独的<not found>值没有问题,那么您可能只需将其转换为:
select col into v_foo from bar where 1=0 -- would provoke ORA-01403
Run Code Online (Sandbox Code Playgroud)
=>没有提出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
Run Code Online (Sandbox Code Playgroud)