ITPub博客

首页 > 数据库 > 数据库开发技术 > Simulate a loop in SQL【By hmxxyy】

Simulate a loop in SQL【By hmxxyy】

原创 数据库开发技术 作者:lastwinner 时间:2005-11-26 21:23:22 0 删除 编辑
SQL> select * from x;

C1 C2
---------- ----------
11111 1
22222 2
33333 3
44444 4
[@more@]

SQL> create table y (c1 number);

Table created.

SQL> insert into y
2 select a.c1 from x a,
3 (
4 select rownum rn from (select max(c2) maxlevel from x) a connect by 1=1 and level <=
5 maxlevel) b
6 where a.c2 >= rn
7 order by c1
8 /

10 rows created.

SQL> select * from y;

C1
----------
11111
22222
22222
33333
33333
33333
44444
44444
44444
44444

http://blog.itpub.net/post/4791/27191

偶的方法:
SQL> create table loopx(c1 varchar2(20),c2 number(3));

表已创建。

SQL> insert into loopx select lpad(rownum,5,rownum),rownum from dual connect by
rownum<5;

已创建4行。

SQL> select * from loopx;

C1 C2
-------------------- ----------
11111 1
22222 2
33333 3
44444 4

SQL> select distinct a.c1,level from loopx a connect by level<=c2;

C1 LEVEL
-------------------- ----------
11111 1
22222 1
22222 2
33333 1
33333 2
33333 3
44444 1
44444 2
44444 3
44444 4

已选择10行。

不明白他为什么要写那么复杂,不过我用了distinct,过滤了大量的记录,也许这种方法效率低吧

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

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

注册时间:2007-12-12

  • 博文量
    223
  • 访问量
    2882767