码迷,mamicode.com
首页 > 数据库 > 详细

pl/sql(四) 游标

时间:2018-05-22 20:40:13      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:color   art   迭代器   变量   and   lin   实现   大于   set   

游标:相当于java中的迭代器,处理在pl/sql中的多条查询结果

要求: 打印出 80 部门的所有的员工的工资:salary: xxx
 
declare
  --1. 定义游标
  cursor salary_cursor is select salary from employees where department_id = 80;
  v_salary employees.salary%type;
begin
 --2. 打开游标
 open salary_cursor;

 --3. 提取游标
 fetch salary_cursor into v_salary;
 
 --4. 对游标进行循环操作: 判断游标中是否有下一条记录
while salary_cursor%found loop
      dbms_output.put_line(salary:  || v_salary);
      fetch salary_cursor into v_salary;
end loop;  
 
 --5. 关闭游标
 close  salary_cursor;
end;

查询80号部门的薪资与名字

 

declare 
 
  v_sal employees.salary%type;
  v_last_name employees.last_name%type;
  cursor emp_sal_cusor  is select salary,last_name from employees where department_id=80;
begin
  open emp_sal_cusor;
  fetch emp_sal_cusor into v_sal,v_last_name;
  while emp_sal_cusor%found loop 
    dbms_output.put_line(v_sal||,||v_last_name);
    fetch emp_sal_cusor into v_sal,v_last_name;
    end loop;
    close emp_sal_cusor;
  
end;

 

游标for循环相比于while 更简便

 

declare 
     
  v_sal employees.salary%type;
  v_last_name employees.last_name%type;
  cursor emp_sal_cusor  is select salary,last_name into v_sal,v_last_name from employees where department_id=80;
begin
  
  for c in emp_sal_cusor loop
   dbms_output.put_line(c.salary||,||c.last_name);
   end loop;
end;

 

例题:

14. 利用游标, 调整公司中员工的工资:

工资范围 调整基数
0 - 5000 5%
5000 - 10000 3%
10000 - 15000 2%
15000 - 1%

 

declare 
  cursor emp_sal_cursor is select employee_id,salary from employees;
  
  v_temp number(4,2);
  
  v_empid employees.employee_id%type;
  v_sal employees.salary%type;
 begin
   open emp_sal_cursor;
   
   fetch emp_sal_cursor into v_empid,v_sal;
   while  emp_sal_cursor%found loop
     if v_sal<5000 then  v_temp:=0.05;
     elsif v_sal<10000 then v_temp:=0.03;
     elsif v_sal<15000 then v_temp:=0.02;
     else v_temp:=0.01;
     end if ;
     dbms_output.put_line(v_empid||,||v_sal);
     update employees 
     set salary=salary*(1+v_temp)
     where employee_id=v_empid; 
      fetch emp_sal_cursor into v_empid,v_sal;
      end loop;
      close  emp_sal_cursor;     
end;

 

for循环实现

 

declare 
  cursor emp_sal_cursor is select employee_id,salary from employees;
  v_temp number(4,2);
begin
      for  c in emp_sal_cursor loop
        if c.salary<5000 then  v_temp:=0.05;
        elsif c.salary<10000 then v_temp:=0.03;
        elsif c.salary<15000 then v_temp:=0.02;
        else v_temp:=0.01;
        end if ;
     update employees 
     set salary=salary*(1+v_temp)
     where employee_id=c.employee_id; 
     end loop;
    
end;

 

带参数的游标

例子:查询80号部门工资大于3000的人

 

declare
    --定义游标
    cursor emp_sal_cursor(dept_id number, sal number) is 
           select salary + 1000 sal, employee_id id 
           from employees 
           where department_id = dept_id and salary > sal;
    
    --定义基数变量
    temp number(4, 2);
begin
    --处理游标的循环操作
    for c in emp_sal_cursor(sal => 4000, dept_id => 80) loop
          --判断员工的工资, 执行 update 操作
          --dbms_output.put_line(c.id || :  || c.sal);
          
          if c.sal <= 5000 then
             temp := 0.05;
          elsif c.sal <= 10000 then
             temp := 0.03;   
          elsif c.sal <= 15000 then
             temp := 0.02;
          else
             temp := 0.01;
          end if;
          
          dbms_output.put_line(c.sal || :  || c.id || ,  || temp);
          --update employees set salary = salary * (1 + temp) where employee_id = c.id;
    end loop;
end;

 

隐式游标:

例子:更新指定员工 salary(涨工资 10),如果该员工没有找到,则打印”查无此人” 信息

 

begin 
    update employees
    set salary=salary+10
    where employee_id=1001;
    
    if sql%notfound then dbms_output.put_line(查无此人);
    end if;
end;

 

pl/sql(四) 游标

标签:color   art   迭代器   变量   and   lin   实现   大于   set   

原文地址:https://www.cnblogs.com/h-dada/p/9073723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!