Oracle游標練手實例


--聲明游標;CURSOR cursor_name IS select_statement

--For循環游標
--(1)定義游標
--(2)定義游標變量
--(3)使用for循環來使用這個游標
declare
  --類型定義
  cursor c_job is
  select empno,ename,job,sal from emp where job='MANAGER';
  --定義一個游標變量v_cinfo c_emp%ROWTYPE,該類型為游標c_emp中的一行數據類型
  c_row c_job%rowtype;
begin
  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
  --類型定義
  cursor c_job is
  select empno,ename,job,sal from emp where job='MANAGER';
  --定義一個游標變量
  c_row c_job%rowtype;
begin
   open c_job;
     loop
       --提取一行數據到c_row
       fetch c_job into c_row;
       --判斷是否提取到值,沒取到值就退出
       --取到值c_job%notfound是false
       --取不到值c_job%notfound是true
       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;          


--1:任意執行一個update操作,用隱式游標sql的屬性%found,%notfound,%rowcount,%isopen觀察update語句的執行情況。  
begin
   update emp set ENAME='ALEARK' WHERE EMPNO=7469;
   if sql%isopen then
      dbms_output.put_line('Openging');
   else
      dbms_output.put_line('closing');
   end if;
   if sql%found then
      dbms_output.put_line('游標指向了有效行');--判斷游標是否指向有效行
   else
      dbms_output.put_line('Sorry');
   end if;
   if sql%notfound then
      dbms_output.put_line('Also Sorry');
   else
      dbms_output.put_line('Haha');
   end if;
      dbms_output.put_line(sql%rowcount);
   exception 
      when no_data_found then
         dbms_output.put_line('Sorry No data');
      when too_many_rows then
         dbms_output.put_line('Too Many rows');
end;
                         
declare
  empNumber emp.EMPNO%TYPE;
  empName emp.ENAME%TYPE;
begin
  if sql%isopen then
     dbms_output.put_line('Cursor is opinging');
  else
     dbms_output.put_line('Cursor is Close');
  end if;
  if sql%notfound then
     dbms_output.put_line('No Value');
  else
     dbms_output.put_line(empNumber);
  end if;
     dbms_output.put_line(sql%rowcount);
     dbms_output.put_line('-------------');                 
     select EMPNO,ENAME into  empNumber,empName from emp where EMPNO=7499;
     dbms_output.put_line(sql%rowcount);                 
 if sql%isopen then
     dbms_output.put_line('Cursor is opinging');
 else
     dbms_output.put_line('Cursor is Closing');
 end if;
 if sql%notfound then
     dbms_output.put_line('No Value');
 else
     dbms_output.put_line(empNumber);
 end if;
 exception 
     when no_data_found then
         dbms_output.put_line('No Value');
     when too_many_rows then
         dbms_output.put_line('too many rows');
end;
                             
--2.使用游標和loop循環來顯示所有部門的名稱
--游標聲明
--create or replace procedure T_proc AS
  --如果弄成存儲過程需要將declare注釋掉
declare
   cursor csr_dept is
   --select語句
   select dname from dept;
   --指定行指針,這句話應該是指定和csr_dept行類型相同的變量
   row_dept csr_dept%rowtype;
begin
   --for循環
   for row_dept in csr_dept loop
     dbms_output.put_line('部門名稱:'||row_dept.dname);
   end loop;
end;   
--end T_proc;    

--3.使用游標和while循環來顯示所有部門的地理位置(用%found屬性)
declare
   --游標聲明
   cursor csr_TestWhile is
   --select語句
   select LOC from dept;
   --指定行指針,這句話應該是指定和csr_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;      
   
       

--4.接受用戶輸入的部門編號,用for循環和游標,打印出此部門的所有雇員的所有信息(使用循環游標)
--CURSOR cursor_name[(parameter[,parameter],...)] IS select_statement;
--定義參數的語法如下:Parameter_name [IN] data_type[{:=|DEFAULT} value]
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(30) loop
     dbms_output.put_line('員工號:'||r_emp.empno||';'||'員工名:'||r_emp.ename||';'||'工資:'||r_emp.sal);
   end loop;
end;


--5.向游標傳遞一個工種,顯示此工種的所有雇員的所有信息(使用參數游標)
declare
  cursor
  c_job(p_job nvarchar2)
  is
  select * from emp where JOB=p_job;
  r_job emp%rowtype;
begin
  for r_job in c_job('SALESMAN') loop
     dbms_output.put_line('員工號:'||r_job.empno||'; '||'員工姓名:'||r_job.ename);
  end loop;
end;
 


--6.用更新游標來為雇員加佣金:(用if實現,創建一個與emp表一模一樣的emp1表,對emp1表進行修改操作),並將更新前后的數據輸出出來。
--http://zheng12tian.iteye.com/blog/815770 
drop table emp1 purge;
create table emp1 as select * from emp;

declare 
  cursor csr_emp is
  select a.ename ename,a.sal sal1,b.sal sal2,b.sal/a.sal num from emp a,emp1 b where a.ename=b.ename; 
  row_emp csr_emp%rowtype;
  cursor csr_Update is
  select * from emp1 for update of sal;
  empInfo csr_Update%rowtype;
  saleInfo emp1.SAL%type;
  num number:=0;
begin
  for empInfo in csr_Update LOOP
    if empInfo.sal < 1500 then
      saleInfo:=empInfo.sal*1.2;
    elsif empInfo.sal < 2000 then
      saleInfo:=empInfo.sal*1.5;
    elsif empInfo.sal < 3000 then
      saleInfo:=empInfo.sal*2;
    else 
      saleInfo:=empInfo.sal*3;  
    end if;
    update emp1 set sal=saleInfo where current of csr_Update;
  end loop;
 for row_emp in csr_emp loop
   dbms_output.put_line('姓名:'||row_emp.ename||'--'||'原先工資:'||row_emp.sal1||'--'||'更新后的工資:'||row_emp.sal2||'--'||'更新的倍數:'||row_emp.num);
   num:=num+1;
 end loop;
   dbms_output.put_line('公司員工有:'||num||'');   
end;    

select a.ename,a.sal,b.sal,b.sal/a.sal from emp a,emp1 b where a.ename=b.ename;  



--7.編寫一個PL/SQL程序塊,對名字以'A'或'S'開始的所有雇員按他們的基本薪水(sal)的10%給他們加薪(對emp1表進行修改操作)
drop table emp1 purge;
create table emp1 as select * from emp;

declare
  --定義游標,改游標用於取目前的工資
  cursor csr_StatSal is
  select * from emp1 where ename like 'A%' or ename like 'S%';
  cursor csr_AddSal is
  select * from emp1 where ename like 'A%' or ename like 'S%' for update of sal;
  r_AddSal csr_AddSal%rowtype;
  saleInfo emp1.Sal%TYPE;
  r_StatSal csr_StatSal%rowtype;
begin
  for r_AddSal in csr_AddSal loop
    dbms_output.put_line(r_AddSal.ename||'原來的工資:'||r_AddSal.sal);
    saleInfo:=r_AddSal.sal*1.1;
    update emp1 set sal=saleInfo where current of csr_AddSal;
  end loop;
    dbms_output.put_line('------------------------------------');
  for r_StatSal in csr_StatSal loop
    dbms_output.put_line(r_StatSal.ename||'現在的工資:'||r_StatSal.sal);
  end loop;  
end;



--8.編寫一個PL/SQL程序塊,對所有的salesman增加佣金(comm)500
drop table emp1 purge;
create table emp1 as select * from emp;

declare
  cursor csr_AddComm(p_job nvarchar2) is
  select * from emp1 where job=p_job for update of comm;
  r_AddComm emp1%rowtype;
  commInfo emp1.comm%type;
begin
  for r_AddComm in csr_AddComm('SALESMAN') loop
    dbms_output.put_line(r_AddComm.ename||'原先的佣金:'||r_AddComm.comm);
    commInfo:=r_AddComm.comm+500;
    update emp1 set comm=commInfo where current of csr_AddComm;    
    dbms_output.put_line(r_AddComm.ename||'現在的佣金:'||commInfo);  
    dbms_output.put_line('------------------------------------');
  end loop;
end;    
  


--9.編寫一個PL/SQL程序塊,以提升2個資格最老的職員為MANAGER(工作時間越長,資格越老)
--(提示:可以定義一個變量作為計數控制游標只提取兩條數據;也可以在聲明游標的時候把雇員中資格最老的兩個查出來放到游標中。)
drop table emp1 purge;
create table emp1 as select * from emp;


declare
  cursor crs_testComput is
  select * from emp1 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);
          --計速器
          top_two:=top_two-1;
           fetch crs_testComput into r_testComput;
      end loop;
  close crs_testComput;
end;


--另外一種使用for的方法
declare
  cursor crs_testComput is
  select * from (select * from emp1 order by hiredate asc) where rownum <=2 ;
  r_num crs_testComput%rowtype;
begin
  for r_num in crs_testComput loop
    dbms_output.put_line('員工姓名:'||r_num.ename||',他的加入公司的時間:'||to_char(r_num.hiredate,'YYYY/MM/DD'));
  end loop;
end;                   



--10.編寫一個PL/SQL程序塊,對所有雇員按他們的基本薪水(sal)的20%為他們加薪,
--如果增加的薪水大於300就取消加薪(對emp1表進行修改操作,並將更新前后的數據輸出出來)
drop table emp1 purge;
create table emp1 as select * from emp;
declare
  cursor crs_UpdateSal is
  select * from emp1 for update of SAL;
  r_UpdateSal crs_UpdateSal%rowtype;
  salAdd emp1.sal%type;
  salInfo emp1.sal%type;
begin
  for r_UpdateSal in crs_UpdateSal 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;
  end loop;
end;       
 

--11.將每位員工工作了多少年零多少月零多少天輸出來。
--近似
  --CEIL(n)函數:取大於等於數值n的最小整數;
  --FLOOR(n)函數:取小雨等於數值n的最大整數;
  --truc的用法 http://publish.it168.com/2005/1028/20051028034101.shtml
drop table emp1 purge;
create table emp1 as select * from emp;
declare
  cursor crs_WorkDay is
  select ename,hiredate,trunc(months_between(sysdate,hiredate)/12) as SPANDYEARS,
    trunc(mod(months_between(sysdate,hiredate),12)) as months,
    trunc(mod(mod(sysdate - hiredate,365),12)) as days
  from emp1;
  r_WorkDay crs_WorkDay%rowtype;
begin
  for r_WorkDay in crs_WorkDay loop
    dbms_output.put_line(r_WorkDay.ename||'已經工作了'||r_WorkDay.SPANDYEARS||'年,零'||r_WorkDay.months||'月,零'||r_WorkDay.days||'');
  end loop;
end;
                               


--12.輸入部門編號,按照下列加薪比例執行(用CASE實現,創建一個emp1表,修改emp1表的數據),並將更新前后的數據輸出來。
-- deptno raise(%)
-- 10     5%
-- 20     10%
-- 30     15%
-- 40     20%
--加薪比例以現有的sal為標准
--CASE expr WHEN comparison_expr THEN return_expr
--[,WHEN comparison_expr THEN return_expr]... [ELSE else_expr] END
drop table emp1 purge;
create table emp1 as select * from emp;

declare
  cursor crs_caseTest is
  select * from emp1 for update of sal;
  r_caseTest crs_caseTest%rowtype;
  salInfo emp1.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 emp1 set sal=salInfo where current of crs_caseTest;
       dbms_output.put_line(r_caseTest.ename||'加工資前的工資為:'||r_caseTest.sal||''||'加完工資后:'||salInfo);
  end loop;
end; 


--13.對每位員工的薪水進行判斷,如果該員工薪水高於其所在部門的平均薪水,則將其薪水減50元,輸出更新前后的薪水,員工姓名,所在部門的編號。
--AVG([distinct|all] expr) over (analytic_clause)
--作用:
--按照analytic_clause中的規則求分組平均值。
  --分析函數語法:
  --FUNCTION_NAME(<argument>,<argument>...)
  --OVER
  --(<Partition-Clause><Order-by-Clause><Windowing Clause>)
     --PARTITION子句
     --按照表達式分區(就是分組),如果省略了分區子句,則全部的結果集被看作是一個單一的組
drop table emp1 purge;
create table emp1 as select * from emp;
DECLARE
     CURSOR 
     crs_testAvg
     IS
     select EMPNO,ENAME,JOB,SAL,DEPTNO,AVG(SAL) OVER (PARTITION BY DEPTNO ) AS DEP_AVG
     FROM EMP1 for update of SAL;
     r_testAvg crs_testAvg%rowtype;
     salInfo emp1.sal%type;
     begin
     for r_testAvg in crs_testAvg loop
     if r_testAvg.SAL>r_testAvg.DEP_AVG then
     salInfo:=r_testAvg.SAL-50;
     else
     salInfo:=r_testAvg.SAL;
     end if;
     update emp1 set SAL=salInfo where current of crs_testAvg;
     dbms_output.put_line(r_testAvg.ename||'調整前工資為:'||r_testAvg.sal||''||'調整后工資:'||salInfo);
     end loop;
end;     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM