Oracle 顯式遊標
阿新 • • 發佈:2018-12-09
-- 查詢員工號,姓名,職位 declare -- 定義遊標 cursor mycur is select empno,ename,job from emp; v_empno emp.empno%type; v_ename emp.ename%type; v_job emp.job%type; begin -- 開啟遊標 open mycur; -- 提取資料 loop fetch mycur into v_empno,v_ename,v_job; dbms_output.put_line('員工編號:'||v_empno||'員工名稱:'||v_ename||'員工職位:'||v_job); -- 什麼時候能夠退出迴圈 -- exit when mycur%notfound; -- exit when not mycur%found; exit when mycur%rowcount=5; end loop; -- 關閉遊標 close mycur; exception when others then dbms_output.put_line('出錯'||sqlerrm); end; -- 檢測遊標是否開啟 declare cursor mycur is select empno, ename, job from emp; v_empno emp.empno%type; v_ename emp.ename%type; v_job emp.job%type; begin open mycur; -- 檢測遊標是否開啟 if mycur%isopen then dbms_output.put_line('遊標已經開啟'); else dbms_output.put_line('遊標沒有開啟'); end if; close mycur; exception when others then dbms_output.put_line('出錯' || sqlerrm); end; -- 按員工的職稱漲工資,總裁漲1000元,經理漲500元,其他員工漲300元 declare cursor mycur is select empno,job from empnew; v_empno empnew.empno%type; v_job empnew.job%type; begin open mycur; loop fetch mycur into v_empno,v_job; if v_job='董事長' then update empnew set sal = sal + 1000 where empno = v_empno; elsif v_job='經理' then update empnew set sal = sal + 500 where empno = v_empno; else update empnew set sal = sal + 300 where empno = v_empno; end if; exit when mycur%notfound; end loop; commit; close mycur; exception when others then rollback; dbms_output.put_line('出錯:'||sqlerrm); end; select * from empnew;