简介:游标的使用,处理多行数据(类似于java中的Iterator迭代器,用于集合的遍历),游标基本不处理,直接把结果返回给程序处理,例如java的list 4.1显示游标 (1)定义游标 (2)打开游标 (3)提取游标 (4)对游标进行循环操作:判断游标中是否有下一条记录 (5)关闭游标 4.2隐式游标游标判断:游标属性
%FOUND 布尔型属性,当最近一次读记录时成功返回,则值为TRUE;
%NOTFOUND 布尔型属性,与%FOUND相反;
%ISOPEN 布尔型属性,当游标已打开时返回TRUE;
%ROWCOUNT 数字型属性,返回已从游标中读取的记录数;
1.基本使用方法,while循环
--要求: 打印出 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;
2.游标结合记录类型使用方法,while循环
--要求: 打印出 80 部门的所有的员工的工资:salary: xxx
declare
--声明一个记录类型
type emp_record is record(
v_salary employees.salary%type,
v_employee_id employees.employee_id%type
);
v_emp_record emp_record;
--1. 定义游标
cursor salary_cursor is select salary,employee_id from employees where department_id = 80;
begin
--2. 打开游标
open salary_cursor;
--3. 提取游标
fetch salary_cursor into v_emp_record;
--4. 对游标进行循环操作: 判断游标中是否有下一条记录
while salary_cursor%found loop
dbms_output.put_line('id:'||v_emp_record.v_employee_id||'salary: ' || v_emp_record.v_salary);
fetch salary_cursor into v_emp_record;
end loop;
--5. 关闭游标
close salary_cursor;
end;
3.游标的for循环 pl/sql语言提供给了游标的for循环语句,自动执行游标的open、fetch、close语句和循环语句的功能;当进入循环时,游标for循环语句自动打开游标,并提取第一行游标的数据,当程序处理完当前所提取的数据而进入下一次循环时,游标for循环语句自动提取下一行数据供程序处理,当提取完结果集合中的所有数据后结束循环,并自动关闭游标. 格式:v_emp_record 为 emp_cursor 游标中的字段
for v_emp_record in emp_cursor loop
程序处理语句 end loop;
--打印出 manager_id 为 100 的员工的 last_name, email, salary 信息(使用游标, 记录类型)
--(法一:使用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;
--(法二:使用while循环)
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;
4.带参数的游标
--带参数的游标
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;
5.隐式游标 sql%notfound 判断
-- 隐式游标: 更新指定员工 salary(涨工资 10),如果该员工没有找到,则打印”查无此人” 信息
begin
update employees set salary = salary + 10 where employee_id = 1005;
if sql%notfound then
dbms_output.put_line('查无此人!');
end if;
end;