Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1182504
  • 博文数量: 398
  • 博客积分: 10110
  • 博客等级: 上将
  • 技术积分: 4055
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-23 20:01
个人简介

新博客http://www.cnblogs.com/zhjh256 欢迎访问

文章分类

全部博文(398)

文章存档

2012年(1)

2011年(41)

2010年(16)

2009年(98)

2008年(142)

2007年(100)

我的朋友

分类: Oracle

2008-01-06 21:11:24

DUAL上的等级查询

select rownum

from dual

connect by level <= N

minus

select col

from my_table

该查询可以用来找出GAP

Oracle 9i (9.2.0)

SQL>SELECT rownum from dual connect by level < 100;

 

    ROWNUM

----------

     1

SQL>select * from (SELECT rownum from dual connect by level < 10)

  2  /

 

    ROWNUM

----------

         1

         2

         3

         4

         5

         6

         7

         8

         9

 

9 rows selected.

Oracle 8i (8.1.7)

    SQL>SELECT rownum from dual connect by level < 10;

 

    ROWNUM

----------

         1

SQL>select * from (SELECT rownum from dual connect by level < 10);

ERROR:

ORA-01436: CONNECT BY loop in user data

 

no rows selected

Oracle 10g

SQL> SELECT rownum from dual connect by level < 10;

 

    ROWNUM

----------

         1

         2

         3

         4

         5

         6

         7

         8

         9

 

9 rows selected

 

当然,也可以使用包,如下:

CREATE OR REPLACE

type typ_num is table of number;

/

 

create or replace package numtest is

  function num(p_num in integer)

  return typ_num;

end numtest;

/

 

create or replace package body numtest is

function num(p_num in integer)

return typ_num is

  n typ_num := typ_num();

begin

  n.extend(p_num);

  return(n);

end num;

end numtest;

/

 

select rownum from table(numtest.num(9));

 

    ROWNUM

----------

         1

         2

         3

         4

         5

         6

         7

         8

         9

但是,在执行explain时,我们会发现select from dual时用的资源会更少。

阅读(439) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~