No_data_found异常处理

时间:2015-03-23 05:28:04

标签: plsql oracle10g oracle-sqldeveloper

PROCEDURE getEmployeeDetails(EmpID     IN NUMBER,
                             EmpSalary OUT NUMBER) Is
  BEGIN

    SELECT Salary
      into EmpSalary
      FROM Employee_accounts_master
     WHERE Emp_ID = EmpID
       AND SALARY = 'B';

  EXCEPTION
    WHEN NO_DATA_FOUND THEN

      dbms_output.put_line(-20001);

当上述查询不提取任何行时,此过程将抛出NO_DATA_FOUND异常。我没有抛出这个异常,而是需要做一些其他的更新/插入过程。如何实现这一目标。

2 个答案:

答案 0 :(得分:1)

测试是否存在记录是显式游标可能有用的一个实例。

PROCEDURE getEmployeeDetails(EmpID     IN NUMBER,
                             EmpSalary OUT NUMBER) 
IS
     cursor c_emp (p_EmpID NUMBER) is
        SELECT Salary
          FROM Employee_accounts_master 
         WHERE Emp_ID = p_EmpID
           AND SALARY = 'B';
     r_Emp c_emp%rowtype;
BEGIN
        open c_emp(EmpID);
        fetch c_emp into r_emp;
        if c_emp%NOTFOUND then
            insert into employee (emp_id) values (EmpID);
        end if;
        close c_emp;
  END;

或者你可以使用MERGE语句,但只编写一个WHEN NOT MATCHED THEN INSERT分支。 Find out more

答案 1 :(得分:-2)

只需在例外部分进行,请查看以下内容:

Begin
BEGIN

        SELECT Salary
          into EmpSalary
          FROM Employee_accounts_master
         WHERE Emp_ID = EmpID
           AND SALARY = 'B';

      EXCEPTION
        WHEN NO_DATA_FOUND THEN
         -- use the label
           goto myinsert;
     end;
    -- here is your line 67 code starts
   <<myinsert>>
    -- your insert statement, for eg.
    insert into emp (empno) values (1);
 end;