在PL/SQL 程序中,对于处理多行记录 的事务经常使用游标来实现。

游标概念

为了处理 SQL 语句,ORACLE 必须分配一个上下文的区域来处理所必需的信息,其中包括要处理的行的数目,一个指向语句被分析以后的表示形式的指针以及查询的活动集。
游标是一个指向上下文的句柄或指针。通过游标,PL/SQL 可以控制上下文区和处理语句时上下文会发生什么事情。
对于不同的SQL 语句,游标的使用情况不同:

SQL语句

游标

非查询语句

隐式的

结果是单行的查询语句

隐式的或显示的

结果是多行的查询语句

显示的

处理显示游标

1.显式游标处理
显式游标处理需四个 PL/SQL 步骤:

  • 定义游标:就是定义一个游标名,以及与其相对应的 SELECT 语句。
    格式:
CURSOR cursor_name[(parameter..)] IS select_statement;

游标参数只能为输入参数,其格式为:

parameter_name [in] datatype [{:= | DEFAULT} expression]

在指定数据类型时,不能使用长度约束。如 NUMBER(5)、CHAR(10) 等都是错误的。

  • 打开游标:就是执行游标所对应的 SELECT 语句,将其查询结果放入工作区,并且指针指向工作区的首部,标识游标结果集合。如果游标查询语句中有 FOR UPDATE 选项,OPEN 语句还将锁定数据表中的游标结果集合对应的数据行。
    格式:
OPEN cursor_name;

在向游标传递参数时,可以使用与函数参数相同的传值方法,即位置表示法和名称表示法。PL/SQL程序不能用OPEN语句重复打开一个游标。

  • 提取游标数据:就是检索结果集合中的数据行,放入指定的输出变量中。格式:
FETCH cursor_name INTO {variable_list | record_variable };
  • 对该记录进行处理
  • 继续处理,知道活动集合中没有记录
  • 关闭游标:当提取和处理完成结果集合数据后,应及时关闭游标,以释放该游标所占用的系统资源,并使该游标的工作区变成无效,不能再使用 FETCH 语句取其中数据。关闭后的游标可以 使用 OPEN 语句重新打开。
    格式:
CLOSE cursor_name;

注:定义的游标不能有 INTO 子句

游标属性:

%FOUND

布尔型属性,当最近一次读记录时成功返回,则值为 TRUE

%NOTFOUND

布尔型属性,与%FOUND 相反

%ISOPEN

布尔型属性,当游标已打开时返回 TRUE

%ROWCOUNT

数字型属性,返回已从游标中读取的记录数

示例

要求: 打印出 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 部门的所有的员工的工资: Xxx 's salary is: xxx

declare
  cursor sal_cursor is select salary ,last_name 
  	from employees where department_id = 80;
  v_sal number(10);
  v_name varchar2(20);
begin
  open sal_cursor;
  fetch sal_cursor into v_sal,v_name;
  while sal_cursor%found loop
        dbms_output.put_line(v_name||'`s salary is '||v_sal);
        fetch sal_cursor into v_sal,v_name;
  end loop;
  
  close sal_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 循环
PL/SQL 语句提供了 游标 FOR 循环语句,自动执行游标的 OPEN、FETCH、CLOSE语句和循环语句的功能,当进入循环时,游标 FOR 循环语句自动打开游标,并提取第一行游标数据,当程序处理完当前所提取的数据而进入下一次循环时,游标 FOR 循环语句自动提取下一行数据提供程序处理,当提取完结果集合中的所有数据行后结束循环,并自动关闭游标。
格式:

FOR index_variable IN cursor_name LOOP
	-- 游标数据处理代码
END LOOP;

其中:
index_variable 为游标 FOR 循环语句隐含声明的索引变量,该变量为记录变量,其结构与游标查询语句返回的结构集合的结构相同。在程序中可以通过引用该索引记录变量元素来读取所提取的游标数据,index_variable 中各元素的名称与游标查询语句选择列表中的所制定的列名相同。如果在游标查询语句的选择列表中存在计算列,则必须为这些计算列指定别名后才能通过游标 FOR 循环语句中的索引标量来访问这些列数据。

注:不要在程序中对游标进行人工操作,不要在程序中定义用于控制 FOR 循环的记录。

(法二:使用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;

PL/SQL 还允许在游标 FOR 循环语句中使用子查询来实现游标的功能。

BEGIN
	for v_emp in (select last_name,salary from employees) loop
		dbms_output.put_line(
			v_emp.last_name ||
			',' || v_emp.salary 
		);
		end loop;
	end;

处理隐式游标
显式游标主要是用于对查询语句的处理,尤其是在查询结果为多条记录的情况下,而对于非查询语句,如修改,删除操作,则由 ORACLE 系统自动地为这些操作设置游标并创建其工作区,这些由系统隐含创建的游标称为隐式游标,隐式游标的名字为SQL,这是由 ORACLE 系统定义的。对于隐式游标的操作,如定义、打开、取值及关闭操作,都由 ORACLE 系统自动地完成,无需用户进行处理。用户只能通过隐式游标的相关属性,来完成相应的操作。在隐式游标的工作区中,所存放的数据是与用户自定义的显示游标无关的、最新处理的一条SQL 语句所包含的数据。
格式调用:

SQL%

隐式游标属性

SQL%FOUND

布尔型属性,当最近一次读记录时成功返回,则值为TRUE

SQL%NOTFOUND

布尔型属性,与SQL%FOUND相反

SQL%ROWCOUNT

数字型属性,返回已从游标中读取得记录数

SQL%ISOPEN

布尔型属性,取值总是FALSE。SQL命令执行完毕立即关闭隐式游标。

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

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