oracle存儲過程的使用
阿新 • • 發佈:2017-06-10
位置 csr lose arr pos trace int spool ins
一. 使用for循環遊標:遍歷全部職位為經理的雇員
1. 定義遊標(遊標就是一個小集合)
2. 定義遊標變量
3. 使用for循環遊標
declare -- 定義遊標c_job cursor c_job is select empno, ename, job, sal from emp where job = ‘MANAGER‘; -- 定義遊標變量c_row c_row c_job%rowtype; begin -- 循環遊標,用遊標變量c_row存循環出的值 for c_row in c_job loop dbms_output.put_line(c_row.empno || ‘-‘ || c_row.ename || ‘-‘ || c_row.job || ‘-‘ || c_row.sal); end loop; end;
二. fetch遊標:遍歷全部職位為經理的雇員
使用的時候必須明白的打開和關閉
declare --定義遊標c_job cursor c_job is select empno, ename, job, sal from emp where job = ‘MANAGER‘; --定義遊標變量c_row c_row c_job%rowtype; begin open c_job; loop --提取一行數據到c_row fetch c_job into c_row; --判讀是否提取到值。沒取到值就退出 exit when c_job%notfound; dbms_output.put_line(c_row.empno || ‘-‘ || c_row.ename || ‘-‘ || c_row.job || ‘-‘ || c_row.sal); end loop; --關閉遊標 close c_job; end;
三. 使用遊標和while循環:遍歷全部部門的地理位置
--3,使用遊標和while循環來顯示全部部門的的地理位置(用%found屬性) declare --聲明遊標 cursor csr_TestWhile is select loc from dept; --指定行指針 row_loc csr_TestWhile%rowtype; begin open csr_TestWhile; --給第一行數據 fetch csr_TestWhile into row_loc; --測試是否有數據,並運行循環 while csr_TestWhile%found loop dbms_output.put_line(‘部門地點:‘ || row_loc.LOC); --給下一行數據 fetch csr_TestWhile into row_loc; end loop; close csr_TestWhile; end;
四. 帶參的遊標:接受用戶輸入的部門編號
declare -- 帶參的遊標 cursor c_dept(p_deptNo number) is select * from emp where emp.deptno = p_deptNo; r_emp emp%rowtype; begin for r_emp in c_dept(20) loop dbms_output.put_line(‘員工號:‘ || r_emp.EMPNO || ‘員工名:‘ || r_emp.ENAME || ‘工資:‘ || r_emp.SAL); end loop; end;
五. 加鎖的遊標:對全部的salesman添加傭金500
declare --查詢數據,加鎖(for update of) cursor csr_addComm(p_job nvarchar2) is select * from emp where job = p_job for update of comm; r_addComm emp%rowtype; commInfo emp.comm%type; begin for r_addComm in csr_addComm(‘SALESMAN‘) loop commInfo := r_addComm.comm + 500; --更新數據(where current of) update emp set comm = commInfo where current of csr_addComm; end loop; end;六. 使用計數器:找出兩個工作時間最長的員工
declare cursor crs_testComput is select * from emp order by hiredate asc; --計數器 top_two number := 2; r_testComput crs_testComput%rowtype; begin open crs_testComput; fetch crs_testComput into r_testComput; while top_two > 0 loop dbms_output.put_line(‘員工姓名:‘ || r_testComput.ename || ‘ 工作時間:‘ || r_testComput.hiredate); --計速器減1 top_two := top_two - 1; fetch crs_testComput into r_testComput; end loop; close crs_testComput; end;七. if/else推斷:對全部員工按基本薪水的20%加薪。假設添加的薪水大於300就取消加薪
declare cursor crs_upadateSal is select * from emp for update of sal; r_updateSal crs_upadateSal%rowtype; salAdd emp.sal%type; salInfo emp.sal%type; begin for r_updateSal in crs_upadateSal loop salAdd := r_updateSal.sal * 0.2; if salAdd > 300 then salInfo := r_updateSal.sal; dbms_output.put_line(r_updateSal.ename || ‘: 加薪失敗。‘ || ‘薪水維持在:‘ || r_updateSal.sal); else salInfo := r_updateSal.sal + salAdd; dbms_output.put_line(r_updateSal.ENAME || ‘: 加薪成功.‘ || ‘薪水變為:‘ || salInfo); end if; update emp set sal = salInfo where current of crs_upadateSal; end loop; end;八. 使用case when:按部門進行加薪
declare cursor crs_caseTest is select * from emp for update of sal; r_caseTest crs_caseTest%rowtype; salInfo emp.sal%type; begin for r_caseTest in crs_caseTest loop case when r_caseTest.deptno = 10 THEN salInfo := r_caseTest.sal * 1.05; when r_caseTest.deptno = 20 THEN salInfo := r_caseTest.sal * 1.1; when r_caseTest.deptno = 30 THEN salInfo := r_caseTest.sal * 1.15; when r_caseTest.deptno = 40 THEN salInfo := r_caseTest.sal * 1.2; end case; update emp set sal = salInfo where current of crs_caseTest; end loop; end;
九. 異常處理:數據回滾
set serveroutput on; declare d_name varchar2(20); begin d_name := ‘developer‘; savepoint A; insert into DEPT values (50, d_name, ‘beijing‘); savepoint B; insert into DEPT values (40, d_name, ‘shanghai‘); savepoint C; exception when others then dbms_output.put_line(‘error happens‘); rollback to A; commit; end; /
十. 基本指令:
set serveroutput on size 1000000 format wrapped; --使DBMS_OUTPUT有效,並設置成最大buffer,防止"吃掉"最前面的空格 set linesize 256; --設置一行能夠容納的字符數 set pagesize 50; --設置一頁有多少行數 set arraysize 5000; --設置來回數據顯示量,這個值會影響autotrace時一致性讀等數據 set newpage none; --頁和頁之間不設不論什麽間隔 set long 5000; --LONG或CLOB顯示的長度 set trimspool on; --將SPOOL輸出中每行後面多余的空格去掉 set timing on; --設置查詢耗時 col plan_plus_exp format a120; --autotrace後explain plan output的格式 set termout off; --在屏幕上暫不顯示輸出的內容,為以下的設置sql做準備 alter session set nls_date_format=‘yyyy-mm-dd hh24:mi:ss‘; --設置時間格式小知識:
以下的語句一定要在Command Window裏面才幹打印出內容
set serveroutput on; begin dbms_output.put_line(‘hello!‘); end; /
oracle存儲過程的使用