Welcome 微信登录

首页 / 数据库 / MySQL / Oracle 学习之:for循环中包涵select语句

Oracle中的for循环用法比较简单,但是在一次用到包涵select语句的for循环时,还是发现了一些自己以前没有注意的东西。
 
我的代码如下:
 
declare
 
val1 date;
val2 date;
begin
 for i in (select empno from emp_s) loop
    select hiredate into val1 from (select empno,hiredate,rank()over(order by hiredate) a from emp_s where empno=i) where a=1;
    select hiredate into val2 from (select empno,hiredate,rank()over(order by hiredate) a from emp_s where empno=i) where a=2;
   dbms_output.put_line(val2-val1);
  end loop;
exception
  when others then
    dbms_output.put_line(sqlerrm);
end;
 
编译的时候发现抛出两处PLS-00382错误。修改后编译通过!(修改处用红字标明!)
 
declare
 
val1 date;
val2 date;
begin
  for i in (select empno from emp_s) loop
    select hiredate into val1 from (select empno,hiredate,rank()over(order by hiredate) a from emp_s where empno=i.empno) where a=1;
    select hiredate into val2 from (select empno,hiredate,rank()over(order by hiredate) a from emp_s where empno=i.empno) where a=2;
   dbms_output.put_line(val2-val1);
  end loop;
exception
  when others then
    dbms_output.put_line(sqlerrm);
end;
 
   从上面的两个例子里面我们可以很明显的看出来,在包涵select语句的for循环中,变量i是被当做表类型来处理的(即使你的select语句中只取了一列数据)。所以当我们把i改成i.empno时编译顺利通过。更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12Oracle 块清除,以及ORA-01555错误Oracle 学习之:ASCII,CHR函数的作用和用法相关资讯      Oracle学习 
  • Oracle学习之数据库数据保存成文件  (01月27日)
  • Oracle学习教程:认识 v$segment_  (05/16/2013 06:01:19)
  • 学习Oracle需要掌握的重要知识点  (06/04/2013 16:27:40)
  • Oracle 学习之:ASCII,CHR函数的  (05/14/2013 19:28:45)
本文评论 查看全部评论 (0)
表情: 姓名: 字数