一. Oracle 游标
-
什么是游标
-
什么是游标
就是一个集合,数组,游标中存储很多的数据,处理多行数据的问题
游标就是指定一条DQL语句,内容就是DQL语句查询出的内容 -
游标的声明: cursor游标的关键字
cursor 游标名 is DQL; -
游标的遍历
a,打开游标
b,提取游标中的内容, 提取一行记录:fetch 游标名 into 变量;
c,循环, 退出,exit when 游标名%notfound; 当游标中没有提取到记录时退出
d,关闭游标 -
栗子:使用游标在控制台打印20号部门所有的员工信息
declare
– 声明游标,把要在控制台显示的数据赋值给游标
cursor cur is select * from emp where deptno = 20;
– 声明一个变量,用来存储游标的一行
e_row emp%rowtype;
begin
– 遍历游标
–a,打开游标
open cur;
loop
–b,提取游标中的内容, 提取一行记录:fetch 游标名 into 变量;
fetch cur into e_row;
exit when cur%notfound;
dbms_output.put_line(e_row.empno || e_row.ename);
–c,循环, 退出,exit when 游标名%notfound; 当游标中没有提取到记录时退出
end loop;
–d,关闭游标
close cur;
end; -
栗子:使用游标给20号部门所有的员工涨工资
declare
cursor cur is select empno,ename from emp where deptno = 20;
begin
– 注意:for循环特点,可以自动打开和关闭游标
– i代表的是游标一行的内容
for i in cur loop
update emp set sal = sal + 1 where empno = i.empno;
end loop;
end;update emp set sal = sal + 1 where deptno = 20;