Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4183871
  • 博文数量: 240
  • 博客积分: 11504
  • 博客等级: 上将
  • 技术积分: 4277
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-28 14:24
文章分类

全部博文(240)

分类: Oracle

2015-12-09 13:40:12

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


点击(此处)折叠或打开

  1. t_girl=# select * from generate_series(1,10);
  2.  generate_series
  3. -----------------
  4.                1
  5.                2
  6.                3
  7.                4
  8.                5
  9.                6
  10.                7
  11.                8
  12.                9
  13.               10
  14. (10 行记录)




  15. 时间:1.290 ms
  16. t_girl=# select * from generate_series(1,10,2);
  17.  generate_series
  18. -----------------
  19.                1
  20.                3
  21.                5
  22.                7
  23.                9
  24. (5 行记录)




  25. 时间:0.431 ms
  26. t_girl=# select * from generate_series(1,10,3);
  27.  generate_series
  28. -----------------
  29.                1
  30.                4
  31.                7
  32.               10
  33. (4 行记录)




  34. 时间:0.879 ms
  35. t_girl=# select * from generate_series(2,10,3);
  36.  generate_series
  37. -----------------
  38.                2
  39.                5
  40.                8
  41. (3 行记录)




  42. 时间:0.867 ms




  43. t_girl=# select count(*) from generate_series(1,1000);
  44.  count
  45. -------
  46.   1000
  47. (1 行记录)




  48. 时间:1.142 ms




  49. t_girl=# select to_char(now() - '1 day'::interval * n,'YYYY-MM-DD') as result from generate_series(1,10) g(n);
  50.    result
  51. ------------
  52.  2015-12-08
  53.  2015-12-07
  54.  2015-12-06
  55.  2015-12-05
  56.  2015-12-04
  57.  2015-12-03
  58.  2015-12-02
  59.  2015-12-01
  60.  2015-11-30
  61.  2015-11-29
  62. (10 行记录)




  63. 时间:17.284 ms





ORACLE:


点击(此处)折叠或打开

  1. 12:10:34 SQL> select * from table(ytt_generate_series(1,10));


  2. COLUMN_VALUE
  3. ------------
  4.            1
  5.            2
  6.            3
  7.            4
  8.            5
  9.            6
  10.            7
  11.            8
  12.            9
  13.           10


  14. 已选择 10 行。


  15. 已用时间: 00: 00: 00.02
  16. 12:10:36 SQL> select * from table(ytt_generate_series(1,10,2));


  17. COLUMN_VALUE
  18. ------------
  19.            1
  20.            3
  21.            5
  22.            7
  23.            9


  24. 已用时间: 00: 00: 00.00
  25. 12:10:54 SQL> select * from table(ytt_generate_series(1,10,3));


  26. COLUMN_VALUE
  27. ------------
  28.            1
  29.            4
  30.            7
  31.           10


  32. 已用时间: 00: 00: 00.00
  33. 12:10:56 SQL> select * from table(ytt_generate_series(2,10,3));


  34. COLUMN_VALUE
  35. ------------
  36.            2
  37.            5
  38.            8


  39. 已用时间: 00: 00: 00.02
  40. 12:11:04 SQL> select count(*) from table(ytt_generate_series(1,1000));


  41.   COUNT(*)
  42. ----------
  43.       1000


  44. 已用时间: 00: 00: 00.13




  45. 13:32:13 SQL> select to_char(sysdate - n,'YYYY-MM-DD') from table(ytt_generate_series(1,10));


  46. TO_CHAR(SY
  47. ----------
  48. 2015-12-08
  49. 2015-12-07
  50. 2015-12-06
  51. 2015-12-05
  52. 2015-12-04
  53. 2015-12-03
  54. 2015-12-02
  55. 2015-12-01
  56. 2015-11-30
  57. 2015-11-29


  58. 已选择 10 行。


  59. 已用时间: 00: 00: 00.01






附上代码:



点击(此处)折叠或打开

  1. -- Declare result set.
  2. create or replace type ytt_num is object
  3. ( n number);
  4. /
  5. create or replace type numbers_table is table of ytt_num;
  6. /
  7. -- Function body.
  8. -- Created by ytt.
  9. -- 2015/12/9
  10. create or replace function ytt_generate_series
  11. (
  12. f_start_num number := 1, -- Start number.
  13. f_end_num number, -- Finish number.
  14. f_step_num number := 1 -- Step.
  15. )
  16. return numbers_table pipelined
  17. is
  18.   list numbers_table := numbers_table();
  19.   i number := 0;
  20.   j number := 1;
  21. begin
  22.   i := f_start_num;
  23.   j := 1;
  24.   -- Increase nested table's size.
  25.   list.extend(f_end_num);
  26.   -- Loop begin.
  27.   while i <= f_end_num loop
  28.   -- Initlization.
  29.     list(j) := ytt_num(null);
  30.     list(j).n := i;
  31.     pipe row(list(j));
  32.     i := i + f_step_num;
  33.     j := j + 1;
  34.   end loop;
  35.   return;
  36. end;
  37. /


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