ORA-01403:未找到任何数据

时间:2015-04-28 06:52:38

标签: sql oracle plsql

我有一个SQL查询,如果输出为NULL,则不应发送警告。

l_sup_id    NUMBER;

begin

SELECT  per_all_assignments_f.supervisor_id
    INTO  l_sup_id
FROM    per_all_assignments_f
WHERE   person_id = p_person_id
    AND     trunc (sysdate) BETWEEN effective_start_date
                    AND     effective_end_date
    AND     primary_flag = 'Y';

elsif (p_person_type = 'APPRAISER' AND  l_sup_id IS NOT NULL) then
        hr_utility.set_message(801, 'HR_51888_APR_APPRAISER_NULL');
        hr_utility.raise_error;
end if;

每当l_sup_id根据逻辑

为空时
  hr_utility.set_message(801, 'HR_51888_APR_APPRAISER_NULL');
不应该执行

但是每当l_sup_id为null我就会得到

  

ORA-01403:未找到数据

日志中的错误

如果l_sup_id不为null应用程序正常工作

3 个答案:

答案 0 :(得分:0)

由于错误表明其指向NO DATA的条件,要处理您需要使用"异常处理",请参阅代码以了解更改,

declare
l_sup_id    NUMBER;

begin

SELECT  per_all_assignments_f.supervisor_id
    INTO  l_sup_id
FROM    per_all_assignments_f
WHERE   person_id = p_person_id
    AND     trunc (sysdate) BETWEEN effective_start_date
                    AND     effective_end_date
    AND     primary_flag = 'Y';

elsif (p_person_type = 'APPRAISER' AND  l_sup_id IS NOT NULL) then
        hr_utility.set_message(801, 'HR_51888_APR_APPRAISER_NULL');
        hr_utility.raise_error;
end if;
--changes start
exception
when no_Data_found then
dbms_output.put_line('No data exists for lsup '|| l_sup_id)

--changes end
end;

修改

   SQL> SELECT * FROM TESTEMP WHERE EMPNO=6677;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      6677 JUPITER    CLERK           7902 17-DEC-80        800                    20

SQL>
SQL>  declare
  2   l_count number;
  3   begin
  4   select comm into l_count from testemp where empno=6677 ;
  5   DBMS_OUTPUT.PUT_LINE('lcount is'||l_count);
  6   if (l_count IS NULL) then
  7   dbms_output.put_line('no data');
  8   else
  9   dbms_output.put_line('data');
 10   end if;
 11   end;
 12  /

lcount is
no data

PL/SQL procedure successfully completed.

如果你比较NULL或者你想根据你的规范使用它,你的情况将不会进入if

答案 1 :(得分:0)

来自文档,

  

NO_DATA_FOUND

     

SELECT INTO语句不返回任何行,或者您的程序引用a   嵌套表中的已删除元素或。中的未初始化元素   索引表。

在PL / SQL代码中, SELECT .. INTO 语句不返回任何行,因此会引发 NO_DATA_FOUND 错误。它永远不会进入下一行,即你的IF-ELSE构造。

如果您想继续操作,则需要优雅地处理 EXCEPTION

例如,

SQL> SET serveroutput ON
SQL>
SQL> DECLARE
  2    o_ename emp.ename%TYPE;
  3    i_empno emp.empno%TYPE;
  4  BEGIN
  5    SELECT ename INTO o_ename FROM emp WHERE empno = i_empno;
  6    -- Handle no_data_found
  7  EXCEPTION
  8  WHEN no_data_found THEN
  9    -- do something
 10    dbms_output.put_line('No records found for employee no '|| i_empno);
 11  END;
 12  /
No records found for employee no

PL/SQL procedure successfully completed.

SQL>

答案 2 :(得分:0)

感谢您的所有回复。 我没有使用普通的sql创建了以下游标

cursor csr_supervisor_id 
                    is
        SELECT  supervisor_id
        FROM    per_all_assignments_f
        WHERE   person_id = p_person_id
            AND     trunc (sysdate) BETWEEN effective_start_date
                    AND     effective_end_date
            AND     primary_flag = 'Y';

并在l_sup_id

中获取它
open csr_supervisor_id;
fetch csr_supervisor_id into l_sup_id;

现在没有收到数据错误。