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;     

 

posted @ 2013-12-05 10:12  I’m Me!  阅读(8824)  评论(0编辑  收藏  举报