Oracle中游標的使用和異常

來源:互聯網
上載者:User

Oracle中游標的使用和異常

  1. 1.沒有參數的游標的使用  
  2. SQL> declare  
  3.   2  cursor cl is select * from emp;  
  4.   3  emp_row emp%rowtype;  
  5.   4  begin  
  6.   5  open cl;  
  7.   6  loop  
  8.   7       fetch cl into emp_row;  
  9.   8       exit when cl%notfound;  
  10.   9       dbms_output.put_line('員工的名稱'|| emp_row.ename || '員工的部門'|| emp_row.deptno);  
  11.  10  end loop;  
  12.  11  close cl;  
  13.  12  end;  
  14.  13  /  
  15.    
  16. 員工的名稱SMITH員工的部門20  
  17. 員工的名稱ALLEN員工的部門30  
  18. 員工的名稱WARD員工的部門30  
  19. 員工的名稱JONES員工的部門20  
  20. 員工的名稱MARTIN員工的部門30  
  21. 員工的名稱BLAKE員工的部門30  
  22. 員工的名稱CLARK員工的部門10  
  23. 員工的名稱SCOTT員工的部門20  
  24. 員工的名稱KING員工的部門10  
  25. 員工的名稱TURNER員工的部門30  
  26. 員工的名稱ADAMS員工的部門20  
  27. 員工的名稱JAMES員工的部門30  
  28. 員工的名稱FORD員工的部門20  
  29. 員工的名稱MILLER員工的部門10  
  30.    
  31. PL/SQL procedure successfully completed  
  32.   
  33.   
  34.   
  35. SQL> declare  
  36.   2  cursor cl(emp_name varchar2) is select * from emp where ename=emp_name;  
  37.   3  emp_row emp%rowtype;  
  38.   4  begin  
  39.   5  open cl('SMITH');  
  40.   6  fetch cl into emp_row;  
  41.   7  dbms_output.put_line('員工的名稱'||emp_row.ename||'員工的工作'|| emp_row.job);  
  42.   8  close cl;  
  43.   9  end;  
  44.  10  /  
  45.    
  46. 員工的名稱SMITH員工的工作CLERK  
  47.   
  48.   
  49.   
  50. 部門編號為10的員工  
  51. SQL> declare  
  52.   2  cursor cl(emp_deptno number) is select * from emp where deptno=emp_deptno;  
  53.   3  emp_row emp%rowtype;  
  54.   4  begin  
  55.   5  open cl(10);  
  56.   6  loop  
  57.   7  fetch cl into emp_row;  
  58.   8  exit when cl%notfound;  
  59.   9  dbms_output.put_line( '員工的名稱:'||emp_row.ename);  
  60.  10  end loop;  
  61.  11  close cl;  
  62.  12  end;  
  63.  13  /  
  64.    
  65. 員工的名稱:CLARK  
  66. 員工的名稱:KING  
  67. 員工的名稱:MILLER  
  68.    
  69. PL/SQL procedure successfully completed  
  70.   
  71.   
  72.   
  73.   
  74. 除0的異常  
  75. SQL> declare  
  76.   2  pnum number:=10;  
  77.   3  begin  
  78.   4  pnum:=pnum/0;  
  79.   5  exception  
  80.   6    when zero_divide then  
  81.   7    dbms_output.put_line('除數不能為0');  
  82.   8  end;  
  83.   9  /  
  84.    
  85. 除數不能為0  
  86.    
  87. PL/SQL procedure successfully completed  
  88.   
  89.   
  90. 自訂的例外  
  91. SQL> declare  
  92.   2     cursor cl(emp_no number) is select * from emp where empno=emp_no;  
  93.   3      emprow emp%rowtype;  
  94.   4      no_found exception;  
  95.   5      begin  
  96.   6      open cl(1000);  
  97.   7           fetch cl into emprow;  
  98.   8                 if cl%notfound then raise no_found;  
  99.   9                 end if;  
  100.  10     close cl;  
  101.  11     exception  
  102.  12           when no_found then  
  103.  13                dbms_output.put_line('自己定義的意外');  
  104.  14     end;  
  105.  15  /  
  106.    
  107. 自己定義的意外  
  108.    
  109. PL/SQL procedure successfully completed  

更多Oracle相關資訊見Oracle 專題頁面 http://www.bkjia.com/topicnews.aspx?tid=12

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.