可以用Oracle已經存在的帳號scott密碼triger登陸進去用裡面已存在的表來做實驗。
- create or replace procedure lpmtest2
- as
- para1 varchar2(10);
- cursor youbiao is select ename from test where sal>1300;
- begin
- open youbiao;
- loop
- fetch youbiao into para1;
- exit when youbiao%notfound;
- dbms_output.put_line('++:'||para1);
- end loop;
- close youbiao;
- end;
代碼
- create or replace procedure lpmtest2
- as
- cursor youbiao is select ename,sal,job from test where sal>1300;
- c_row youbiao%rowtype; --定義一個遊標變數c_row ,該類型為遊標youbiao中的一行資料類型
- begin
- open youbiao;
- loop
- fetch youbiao into c_row;
- exit when youbiao%notfound;
- dbms_output.put_line('++:'||c_row.ename||':'||c_row.sal||':'||c_row.job);
- end loop;
- close youbiao;
- end;
代碼
- create or replace procedure lpmtest3
- as
- cursor c_dept is select * from dept order by deptno;
- cursor c_emp(p_dept varchar2) is select ename,sal from emp where deptno=p_dept order by ename;
- r_dept c_dept%rowtype;
- v_ename emp.ename%type;
- v_sal emp.sal%type;
- v_totalsal emp.sal%type; --用來存每個部門所有員工的總工資
- begin
- open c_dept;
- loop
- fetch c_dept into r_dept;
- exit when c_dept%notfound;
- dbms_output.put_line(r_dept.deptno||':'||r_dept.dname||'+++++++++++');
- v_totalsal:=0;
- open c_emp(r_dept.deptno);
- loop
- fetch c_emp into v_ename,v_sal;
- exit when c_emp%notfound;
- dbms_output.put_line('v_ename:'||v_ename||';'||'v_sal:'||v_sal);
- v_totalsal:=v_totalsal+v_sal;
- end loop;
- close c_emp;
- dbms_output.put_line('deptsaltotal:'||v_totalsal);
- end loop;
- close c_dept;
- end;
列印出來效果:
- 10:ACCOUNTING+++++++++++
- v_ename:CLARK;v_sal:2450
- v_ename:KING;v_sal:5000
- v_ename:MILLER;v_sal:1300
- deptsaltotal:8750
- 20:RESEARCH+++++++++++
- v_ename:ADAMS;v_sal:1100
- v_ename:FORD;v_sal:3000
- v_ename:JONES;v_sal:2975
- v_ename:SCOTT;v_sal:3000
- v_ename:SMITH;v_sal:800
- deptsaltotal:10875
- 30:SALES+++++++++++
- v_ename:ALLEN;v_sal:1600
- v_ename:BLAKE;v_sal:2850
- v_ename:JAMES;v_sal:950
- v_ename:MARTIN;v_sal:1250
- v_ename:TURNER;v_sal:1500
- v_ename:WARD;v_sal:1250
- deptsaltotal:9400
- 40:OPERATIONS+++++++++++
- deptsaltotal:0
更多Oracle相關資訊見Oracle 專題頁面 http://www.bkjia.com/topicnews.aspx?tid=12