通过游标处理select into只能处理返回一行
在变量赋值的方法中,select into只能为返回单行数据的变量进行赋值
SQL> set serveroutput on SQL> declare 2 v_date date; 3 begin 4 select hire_date into v_date from employees where rownum=1; 5 dbms_output.put_line(v_date); 6 end; 7 / 21-JUN-99 PL/SQL procedure successfully completed.
当我们尝试使用select into为变量赋值多个数据,就会报错,如下
SQL> set serveroutput on SQL> declare 2 v_date date; 3 begin 4 select hire_date into v_date from employees; 5 dbms_output.put_line(v_date); 6 end; 7 / declare * ERROR at line 1: ORA-01422: exact fetch returns more than requested number of rows ORA-06512: at line 4
使用显示游标处理select into只能返回单行数据为变量赋值
SQL> set serveroutput on SQL> declare 2 cursor c_date is 3 select hire_date from employees; 4 v_date date; 5 begin 6 open c_date; 7 loop 8 fetch c_date into v_date; 9 exit when c_date%notfound; 10 dbms_output.put_line(v_date); 11 end loop; 12 close c_date; 13 end; / 14 21-JUN-99 13-JAN-00 17-SEP-87 17-FEB-96 17-AUG-97 07-JUN-94 07-JUN-94 07-JUN-94 07-JUN-94 PL/SQL procedure successfully completed.