exception

SQL> -- 例外 exception
SQL> -- 1/0
SQL> ed
已寫入 file afiedt.bufide

  1  declare
  2   pnum number := 0;
  3  begin
  4   pnum := 1/pnum; -- 會跑出一個叫zero_divide的異常
  5  exception
  6   when zero_divide then dmms_output.put_line('不能被0除');
  7* end;
  8  ed
  9  /
ed
 *
第 8 行出現錯誤:
ORA-06550: 第 8 行, 第 1 列:
PLS-00103: 出現符號 "ED"符號 "ED" 被忽略。 oop


SQL> ed
已寫入 file afiedt.buffetch

  1  declare
  2   pnum number := 0;
  3  begin
  4   pnum := 1/pnum; -- 會跑出一個叫zero_divide的異常
  5  exception
  6   when zero_divide then dbms_output.put_line('不能被0除');
  7* end;
SQL> /
不能被0除                                                                                                                                                                                               server

PL/SQL 過程已成功完成。it

SQL> ed
已寫入 file afiedt.bufio

  1  declare
  2   pnum number := 0;
  3  begin
  4   pnum := 1/pnum; -- 會跑出一個叫zero_divide的異常
  5* end;
  6  /
declare
*
第 1 行出現錯誤:
ORA-01476: 除數爲 0
ORA-06512: 在 line 4 file


SQL> ed
已寫入 file afiedt.bufselect

  1  declare
  2   pnum number := 0;
  3  begin
  4   pnum := 1/pnum; -- 會跑出一個叫zero_divide的異常
  5  exception
  6   when zero_divide then dbms_output.put_line('不能被0除')
  7* end;
SQL> /
end;
   *
第 7 行出現錯誤:
ORA-06550: 第 7 行, 第 1 列:
PLS-00103: 出現符號 "END"在須要下列之一時:
:= . ( % ;
符號 ";" 被替換爲 "END" 後繼續。 exception


SQL> ed
已寫入 file afiedt.buf請求

  1  declare
  2   pnum number := 0;
  3  begin
  4   pnum := 1/pnum; -- 會跑出一個叫zero_divide的異常
  5  exception
  6   when zero_divide then dbms_output.put_line('不能被0除');
  7* end;
SQL> /
不能被0除                                                                                                                                                                                              

PL/SQL 過程已成功完成。

SQL> ed
已寫入 file afiedt.buf

  1  declare
  2   pnum number := 0;
  3   pname emp.ename%type;
  4  begin
  5   select ename into pname from emp;
  6   pnum := 1/pnum; -- 會跑出一個叫zero_divide的異常
  7  exception
  8   when zero_divide then dbms_output.put_line('不能被0除');
  9* end;
SQL> /
declare
*
第 1 行出現錯誤:
ORA-01422: 實際返回的行數超出請求的行數
ORA-06512: 在 line 5


SQL> ed
已寫入 file afiedt.buf

  1  declare
  2   pnum number := 0;
  3   pname emp.ename%type;
  4  begin
  5   select ename into pname from emp;
  6   pnum := 1/pnum; -- 會跑出一個叫zero_divide的異常
  7  exception
  8   when zero_divide then dbms_output.put_line('不能被0除');
  9   when too_many_rows then dbms_output.put_line('太多的行');
 10* end;
SQL> /
太多的行                                                                                                                                                                                               

PL/SQL 過程已成功完成。

SQL> ed
已寫入 file afiedt.buf

  1  declare
  2   pnum number := 0;
  3   pname emp.ename%type;
  4  begin
  5   select ename into pname from emp;
  6   pnum := 1/pnum; -- 會跑出一個叫zero_divide的異常
  7  exception
  8   when zero_divide then dbms_output.put_line('不能被0除');
  9   when too_many_rows then dbms_output.put_line('太多的行');
 10   when others then dbms_output.put_line('qita');
 11* end;
SQL> /
太多的行                                                                                                                                                                                               

PL/SQL 過程已成功完成。

SQL> ed
已寫入 file afiedt.buf

  1  declare
  2   pnum number := 0;
  3   pname emp.ename%type;
  4  begin
  5   select ename into pname from emp;
  6   pnum := 1/pnum; -- 會跑出一個叫zero_divide的異常
  7  exception
  8   when zero_divide then dbms_output.put_line('不能被0除');
  9   when others then dbms_output.put_line('qita');
 10* end;
SQL> /
qita                                                                                                                                                                                                   

PL/SQL 過程已成功完成。

SQL> -- 自定義列外
SQL> ed
已寫入 file afiedt.buf

  1  -- 查詢50號部門的全部員工的姓名,若是沒有查到數據跑出列外
  2  declare
  3   cursor cemp is select ename from emp where deptno = 50;
  4   pname emp.ename%type;
  5   -- 本身定義異常
  6   not_emp_data exception;
  7  begin
  8   open cursor;
  9        if cemp%notfound then raise not_emp_data;
 10    else
 11    loop
 12     fetch cemp into pname;
 13     exit when cemp%notfound;
 14     dbms_output.put_line(pname);
 15    end loop;
 16        end if;
 17   close cursor;
 18  exception
 19   when not_emp_data then dbms_output.put_line('沒有找到50號部門的數據');
 20* end;
 21  /
      if cemp%notfound then raise not_emp_data;
                       *
第 9 行出現錯誤:
ORA-06550: 第 8 行, 第 7 列:
PLS-00201: 必須聲明標識符 'CURSOR'
ORA-06550: 第 8 行, 第 2 列:
PL/SQL: SQL Statement ignored
ORA-06550: 第 17 行, 第 8 列:
PLS-00201: 必須聲明標識符 'CURSOR'
ORA-06550: 第 17 行, 第 2 列:
PL/SQL: SQL Statement ignored


SQL> ed
已寫入 file afiedt.buf

  1  -- 查詢50號部門的全部員工的姓名,若是沒有查到數據跑出列外
  2  declare
  3   cursor cemp is select ename from emp where deptno = 50;
  4   pname emp.ename%type;
  5   -- 本身定義異常
  6   not_emp_data exception;
  7  begin
  8   open cemp;
  9        if cemp%notfound then raise not_emp_data;
 10    else
 11    loop
 12     fetch cemp into pname;
 13     exit when cemp%notfound;
 14     dbms_output.put_line(pname);
 15    end loop;
 16        end if;
 17   close cursor;
 18  exception
 19   when not_emp_data then dbms_output.put_line('沒有找到50號部門的數據');
 20* end;
SQL> ed
已寫入 file afiedt.buf

  1  -- 查詢50號部門的全部員工的姓名,若是沒有查到數據跑出列外
  2  declare
  3   cursor cemp is select ename from emp where deptno = 50;
  4   pname emp.ename%type;
  5   -- 本身定義異常
  6   not_emp_data exception;
  7  begin
  8   open cemp;
  9        if cemp%notfound then raise not_emp_data;
 10    else
 11    loop
 12     fetch cemp into pname;
 13     exit when cemp%notfound;
 14     dbms_output.put_line(pname);
 15    end loop;
 16        end if;
 17   close cemp;
 18  exception
 19   when not_emp_data then dbms_output.put_line('沒有找到50號部門的數據');
 20* end;
SQL> /

PL/SQL 過程已成功完成。

SQL> ed
已寫入 file afiedt.buf

  1  -- 查詢50號部門的全部員工的姓名,若是沒有查到數據跑出列外
  2  set serveroutput on
  3  declare
  4   cursor cemp is select ename from emp where deptno = 50;
  5   pname emp.ename%type;
  6   -- 本身定義異常
  7   not_emp_data exception;
  8  begin
  9   open cemp;
 10        if cemp%notfound then raise not_emp_data;
 11    else
 12    loop
 13     fetch cemp into pname;
 14     exit when cemp%notfound;
 15     dbms_output.put_line(pname);
 16    end loop;
 17        end if;
 18   close cemp;
 19  exception
 20   when not_emp_data then dbms_output.put_line('沒有找到50號部門的數據');
 21* end;
SQL> /
 cursor cemp is select ename from emp where deptno = 50;
 *
第 4 行出現錯誤:
ORA-00922: 選項缺失或無效


SQL> ed
已寫入 file afiedt.buf

  1  -- 查詢50號部門的全部員工的姓名,若是沒有查到數據跑出列外
  2  declare
  3   cursor cemp is select ename from emp where deptno = 50;
  4   pname emp.ename%type;
  5   -- 本身定義異常
  6   not_emp_data exception;
  7  begin
  8   open cemp;
  9        if cemp%notfound then raise not_emp_data;
 10    else
 11    loop
 12     fetch cemp into pname;
 13     exit when cemp%notfound;
 14     dbms_output.put_line(pname);
 15    end loop;
 16        end if;
 17   close cemp;
 18  exception
 19   when not_emp_data then dbms_output.put_line('沒有找到50號部門的數據');
 20* end;
SQL> set serveroutput on;
SQL> -- 查詢50號部門的全部員工的姓名,若是沒有查到數據跑出列外
SQL> declare
  2   cursor cemp is select ename from emp where deptno = 50;
  3   pname emp.ename%type;
  4   -- 本身定義異常
  5   not_emp_data exception;
  6  begin
  7   open cemp;
  8        if cemp%notfound then raise not_emp_data;
  9    else
 10    loop
 11     fetch cemp into pname;
 12     exit when cemp%notfound;
 13     dbms_output.put_line(pname);
 14    end loop;
 15        end if;
 16   close cemp;
 17  exception
 18   when not_emp_data then dbms_output.put_line('沒有找到50號部門的數據');
 19  end;
 20  /

PL/SQL 過程已成功完成。

SQL> ed
已寫入 file afiedt.buf

  1  declare
  2   cursor cemp is select ename from emp where deptno = 50;
  3   pname emp.ename%type;
  4   -- 本身定義異常
  5   not_emp_data exception;
  6  begin
  7   open cemp;
  8        if cemp%notfound then
  9    dbms_output.put_line('11');
 10    raise not_emp_data;
 11    else
 12    loop
 13     fetch cemp into pname;
 14     exit when cemp%notfound;
 15     dbms_output.put_line(pname);
 16    end loop;
 17        end if;
 18   close cemp;
 19  exception
 20   when not_emp_data then dbms_output.put_line('沒有找到50號部門的數據');
 21* end;
SQL> /

PL/SQL 過程已成功完成。

SQL> ed
已寫入 file afiedt.buf

  1  declare
  2   cursor cemp is select ename from emp where deptno = 50;
  3   pname emp.ename%type;
  4   -- 本身定義異常
  5   not_emp_data exception;
  6  begin
  7   open cemp;
  8   fetch cemp into pname;
  9        if cemp%notfound then
 10    dbms_output.put_line('11');
 11    raise not_emp_data;
 12    else
 13    loop
 14     exit when cemp%notfound;
 15     dbms_output.put_line(pname);
 16    end loop;
 17        end if;
 18   close cemp;
 19  exception
 20   when not_emp_data then dbms_output.put_line('沒有找到50號部門的數據');
 21* end;
SQL> /
11                                                                                                                                                                                                     
沒有找到50號部門的數據                                                                                                                                                                                 

PL/SQL 過程已成功完成。

SQL> ed
已寫入 file afiedt.buf

  1  declare
  2   cursor cemp is select ename from emp where deptno = 50;
  3   pname emp.ename%type;
  4   -- 本身定義異常
  5   not_emp_data exception;
  6  begin
  7   open cemp;
  8   fetch cemp into pname;
  9        if cemp%notfound then
 10    dbms_output.put_line('11');
 11    raise not_emp_data;
 12    else
 13    loop
 14     exit when cemp%notfound;
 15     dbms_output.put_line(pname);
 16    end loop;
 17        end if;
 18   close cemp;
 19  exception
 20   when not_emp_data then dbms_output.put_line('沒有找到50號部門的數據');
 21* end;
SQL> /
11                                                                                                                                                                                                     
沒有找到50號部門的數據                                                                                                                                                                                 

PL/SQL 過程已成功完成。

SQL> select sum(sal) from emp;

  SUM(SAL)                                                                                                                                                                                             
----------                                                                                                                                                                                             
     47025                                                                                                                                                                                             

SQL> spool off;

相關文章
相關標籤/搜索