Oracle遊標預存程序語句

來源:互聯網
上載者:User

可以用Oracle已經存在的帳號scott密碼triger登陸進去用裡面已存在的表來做實驗。 

  1. create or replace procedure lpmtest2   
  2. as    
  3.   para1 varchar2(10);   
  4.   cursor  youbiao is  select ename from test where sal>1300;   
  5. begin   
  6.   open youbiao;   
  7.   loop   
  8.     fetch youbiao into para1;   
  9.     exit when youbiao%notfound;   
  10.     dbms_output.put_line('++:'||para1);   
  11.   end loop;   
  12.   close youbiao;   
  13. end;  

代碼

  1. create or replace procedure lpmtest2   
  2. as    
  3.   cursor  youbiao is  select ename,sal,job from test where sal>1300;   
  4.   c_row youbiao%rowtype; --定義一個遊標變數c_row ,該類型為遊標youbiao中的一行資料類型   
  5. begin   
  6.   open youbiao;   
  7.   loop   
  8.     fetch youbiao into c_row;   
  9.     exit when youbiao%notfound;   
  10.     dbms_output.put_line('++:'||c_row.ename||':'||c_row.sal||':'||c_row.job);   
  11.   end loop;   
  12.   close youbiao;   
  13. end;  

代碼

  1. create or replace procedure lpmtest3   
  2. as   
  3. cursor c_dept is select * from dept order by deptno;   
  4. cursor c_emp(p_dept varchar2) is select ename,sal from emp where deptno=p_dept order by ename;   
  5. r_dept c_dept%rowtype;   
  6. v_ename emp.ename%type;   
  7. v_sal   emp.sal%type;   
  8. v_totalsal emp.sal%type; --用來存每個部門所有員工的總工資   
  9. begin   
  10.   open c_dept;   
  11.   loop   
  12.     fetch c_dept into r_dept;   
  13.     exit when c_dept%notfound;   
  14.     dbms_output.put_line(r_dept.deptno||':'||r_dept.dname||'+++++++++++');   
  15.     v_totalsal:=0;   
  16.     open c_emp(r_dept.deptno);   
  17.     loop   
  18.       fetch c_emp into v_ename,v_sal;   
  19.       exit when c_emp%notfound;   
  20.       dbms_output.put_line('v_ename:'||v_ename||';'||'v_sal:'||v_sal);   
  21.       v_totalsal:=v_totalsal+v_sal;   
  22.      end loop;   
  23.      close c_emp;   
  24.      dbms_output.put_line('deptsaltotal:'||v_totalsal);   
  25.    end loop;   
  26.    close c_dept;   
  27. end;  

列印出來效果:

  1. 10:ACCOUNTING+++++++++++   
  2. v_ename:CLARK;v_sal:2450  
  3. v_ename:KING;v_sal:5000  
  4. v_ename:MILLER;v_sal:1300  
  5. deptsaltotal:8750  
  6. 20:RESEARCH+++++++++++   
  7. v_ename:ADAMS;v_sal:1100  
  8. v_ename:FORD;v_sal:3000  
  9. v_ename:JONES;v_sal:2975  
  10. v_ename:SCOTT;v_sal:3000  
  11. v_ename:SMITH;v_sal:800  
  12. deptsaltotal:10875  
  13. 30:SALES+++++++++++   
  14. v_ename:ALLEN;v_sal:1600  
  15. v_ename:BLAKE;v_sal:2850  
  16. v_ename:JAMES;v_sal:950  
  17. v_ename:MARTIN;v_sal:1250  
  18. v_ename:TURNER;v_sal:1500  
  19. v_ename:WARD;v_sal:1250  
  20. deptsaltotal:9400  
  21. 40:OPERATIONS+++++++++++   
  22. deptsaltotal:0  
更多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.