ITPub博客

首页 > Linux操作系统 > Linux操作系统 > rownum 详解

rownum 详解

原创 Linux操作系统 作者:chaobaojun 时间:2013-04-07 18:43:52 0 删除 编辑

ROWNUM是一个序列,是oracle数据库从数据文件或缓冲区中读取数据的顺序。它取得第一条记录则rownum值为1,第二条为2,依次类推。
如果 你用>,>=,=,between…and这些条件,因为从缓冲区或数据文件中得到的第一条记录的rownum为1,则被删除,接着取下 条,
可是它的rownum还是1,又被删除,依次类推,便没有了数据。
rownum 的原理

题目:
1.drop table t;
create table t as select rownum rn from dba_objects where rownum <= 100;
2. select * from t where rownum =1 ;
select * from t where rownum >=1 ;
select * from t where rownum >=5 ;
select * from t where rownum =5 ;
select * from t where rownum <=5 ;
rownum是在执行期间生成的数据,没有1,怎么可能有2
rownum在oralce内编写方式
select * from t where rownum =1 ;
begin
rownum = 1 
For x in ( select * from t )   --rownum=1,可以进入循环
Loop 
  if ( rownum = 1 ) 
  then 
     output date 
     rownum = rownum+1 
  end if; 
End loop;
end;
select * from t where rownum >=5 ;  ----因为rownum=1.所以直接退出循环,没有输出
rownum = 1 
For x in ( select * from t ) 
Loop 
  if ( rownum >= 5 ) 
  then 
     output date 
     rownum = rownum+1 
  end if; 
End loop;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21861353/viewspace-757908/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-05-14

  • 博文量
    29
  • 访问量
    67526