首页 > 其他 > 详细

通过游标处理select into只能处理返回一行

时间:2021-01-22 16:55:35      阅读:33      评论:0      收藏:0      [点我收藏+]

在变量赋值的方法中,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.

 

通过游标处理select into只能处理返回一行

原文:https://www.cnblogs.com/hanglinux/p/14313505.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!