Home >Database >Mysql Tutorial >Oracle 学习之:for循环中包涵select语句

Oracle 学习之:for循环中包涵select语句

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 17:28:591435browse

oracle中的for循环用法比较简单,但是在一次用到包涵select语句的for循环时,还是发现了一些自己以前没有注意的东西。

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时编译顺利通过。

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:(Oracle)rownum释疑Next article:Hive 创建表