文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

【原创】Oracle实现PGSQL的generate_series

2024-04-02 19:55

关注

PostgreSQL 有生成序列的函数,可以方便的进行造数据。这里我用ORACLE实现了PGSQL的generate_series函数功能。

POSTGRESQL.

t_girl=# select * from generate_series(1,10);
 generate_series
-----------------
               1
               2
               3
               4
               5
               6
               7
               8
               9
              10
(10 行记录)
时间:1.290 ms
t_girl=# select * from generate_series(1,10,2);
 generate_series
-----------------
               1
               3
               5
               7
               9
(5 行记录)
时间:0.431 ms
t_girl=# select * from generate_series(1,10,3);
 generate_series
-----------------
               1
               4
               7
              10
(4 行记录)
时间:0.879 ms
t_girl=# select * from generate_series(2,10,3);
 generate_series
-----------------
               2
               5
               8
(3 行记录)
时间:0.867 ms
t_girl=# select count(*) from generate_series(1,1000);
 count
-------
  1000
(1 行记录)
时间:1.142 ms
t_girl=# select to_char(now() - '1 day'::interval * n,'YYYY-MM-DD') as result from generate_series(1,10)  g(n);
   result
------------
 2015-12-08
 2015-12-07
 2015-12-06
 2015-12-05
 2015-12-04
 2015-12-03
 2015-12-02
 2015-12-01
 2015-11-30
 2015-11-29
(10 行记录)
时间:17.284 ms


ORACLE:

12:10:34 SQL> select * from table(ytt_generate_series(1,10));
COLUMN_VALUE                                                                    
------------                                                                    
           1                                                                    
           2                                                                    
           3                                                                    
           4                                                                    
           5                                                                    
           6                                                                    
           7                                                                    
           8                                                                    
           9                                                                    
          10                                                                    
已选择 10 行。
已用时间:  00: 00: 00.02
12:10:36 SQL> select * from table(ytt_generate_series(1,10,2));
COLUMN_VALUE                                                                    
------------                                                                    
           1                                                                    
           3                                                                    
           5                                                                    
           7                                                                    
           9                                                                    
已用时间:  00: 00: 00.00
12:10:54 SQL> select * from table(ytt_generate_series(1,10,3));
COLUMN_VALUE                                                                    
------------                                                                    
           1                                                                    
           4                                                                    
           7                                                                    
          10                                                                    
已用时间:  00: 00: 00.00
12:10:56 SQL> select * from table(ytt_generate_series(2,10,3));
COLUMN_VALUE                                                                    
------------                                                                    
           2                                                                    
           5                                                                    
           8                                                                    
已用时间:  00: 00: 00.02
12:11:04 SQL> select count(*) from table(ytt_generate_series(1,1000));
  COUNT(*)                                                                      
----------                                                                      
      1000                                                                      
已用时间:  00: 00: 00.13
13:32:13 SQL> select to_char(sysdate - n,'YYYY-MM-DD') from table(ytt_generate_series(1,10));
TO_CHAR(SY
----------
2015-12-08
2015-12-07
2015-12-06
2015-12-05
2015-12-04
2015-12-03
2015-12-02
2015-12-01
2015-11-30
2015-11-29
已选择 10 行。
已用时间:  00: 00: 00.01



附上代码:

-- Declare result set.
create or replace type ytt_num is object
( n number);
/
create or replace type numbers_table is table of ytt_num;
/
-- Function body.
-- Created by ytt.
-- 2015/12/9
create or replace function ytt_generate_series
(
f_start_num number := 1, -- Start number.
f_end_num number,  -- Finish number.
f_step_num number := 1 -- Step.
)
return numbers_table pipelined
is 
  list numbers_table := numbers_table();
  i number := 0;
  j number := 1;
begin
  i := f_start_num;
  j := 1;
  -- Increase nested table's size.
  list.extend(f_end_num);
  -- Loop begin.
  while i <= f_end_num loop
  -- Initlization.
    list(j) := ytt_num(null);
    list(j).n := i;
    pipe row(list(j));
    i := i + f_step_num;
    j := j + 1;
  end loop;
  return;
end;
/


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯