PL/SQL游标的使用

  1. 游标的使用

游标是在处理多行记录的事务时经常使用。

游标是一个指向上下文的句柄或指针。

2.显式游标

要求: 打印出 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;

打印出 manager_id 为 100 的员工的 last_name, email, salary 信息(使用游标, 记录类型)

declare  
           --声明游标    
           cursor emp_cursor is select last_name, email, salary from employees where manager_id = 100;
           
           --声明记录类型
           type emp_record is record(
                name employees.last_name%type,
                email employees.email%type,
                salary employees.salary%type
           );
           
           -- 声明记录类型的变量
           v_emp_record emp_record;
begin
           --打开游标
           open emp_cursor;
           
           --提取游标
           fetch emp_cursor into v_emp_record;
           
           --对游标进行循环操作
           while emp_cursor%found loop
                  dbms_output.put_line(v_emp_record.name || ', ' || v_emp_record.email || ', ' || v_emp_record.salary );                
                  fetch emp_cursor into v_emp_record;
           end loop;
           
           --关闭游标
           close emp_cursor;
end;

(法二:使用for循环)

declare
   
      cursor emp_cursor is 
      select last_name,email,salary
      from employees
      where manager_id = 100;

begin


      for v_emp_record in emp_cursor loop
          dbms_output.put_line(v_emp_record.last_name||','||v_emp_record.email||','||v_emp_record.salary);
      end loop;
end;

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

工资范围 调整基数

0 - 5000 5%

5000 - 10000 3%

10000 - 15000 2%

15000 - 1%

declare
    --定义游标
    cursor emp_sal_cursor is select salary, employee_id from employees;
    
    --定义基数变量
    temp number(4, 2);
    
    --定义存放游标值的变量
    v_sal employees.salary%type;
    v_id employees.employee_id%type;
begin
    --打开游标
    open emp_sal_cursor;
    
    --提取游标
    fetch emp_sal_cursor into v_sal, v_id;
    
    --处理游标的循环操作
    while emp_sal_cursor%found loop
          --判断员工的工资, 执行 update 操作
          --dbms_output.put_line(v_id || ': ' || v_sal);
            
          if v_sal <= 5000 then
             temp := 0.05;
          elsif v_sal<= 10000 then
             temp := 0.03;   
          elsif v_sal <= 15000 then
             temp := 0.02;
          else
             temp := 0.01;
          end if;
          
          --dbms_output.put_line(v_id || ': ' || v_sal || ', ' || temp);
          update employees set salary = salary * (1 + temp) where employee_id = v_id; 
                  
          fetch emp_sal_cursor into v_sal, v_id;
    end loop;
    --关闭游标
    close emp_sal_cursor;
end;

利用游标 for 循环完成

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

带参数的游标

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 = 1005;
         
         if sql%notfound then
            dbms_output.put_line('查无此人!');
         end if;
end;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值