| Puzzle n°00 - Row generator * [message #291171] |
Thu, 03 January 2008 04:37  |
Michel Cadot Messages: 17653 Registered: March 2007 Location: Nanterre, France, http://... |
Senior Member |
|
|
The purpose of this puzzle is to wrap-up all SQL or PL/SQL techniques to generate rows.
Enjoy!
Regards
Michel
|
|
|
| Re: Puzzle n°00 - Row generator * [message #291177 is a reply to message #291171 ] |
Thu, 03 January 2008 04:52   |
Michel Cadot Messages: 17653 Registered: March 2007 Location: Nanterre, France, http://... |
Senior Member |
|
|
SQL 8i
SQL> DEFINE N=3
SQL> SELECT ROWNUM
2 FROM ( SELECT 1 FROM DUAL GROUP BY CUBE(1,2,3,4,5,6,7,8,9,10) )
3 WHERE ROWNUM <= &N
4 /
ROWNUM
----------
1
2
3
SQL 9i
SQL> SELECT *
2 FROM ( SELECT ROWNUM FROM DUAL CONNECT BY LEVEL <= &N )
3 /
ROWNUM
----------
1
2
3
SQL 10g
SQL> SELECT ROWNUM FROM DUAL CONNECT BY LEVEL <= &N;
ROWNUM
----------
1
2
3
Using a pipeline function (9i)
SQL> create or replace type row_table as table of number;
2 /
Type created.
SQL> Create or replace function gen_row (num_rows in number)
2 return row_table
3 parallel_enable pipelined is
4 begin
5 for x in 1..num_rows loop
6 pipe row (x);
7 end loop;
8 return;
9 end;
10 /
Function created.
SQL> select * from table(gen_row(&N));
COLUMN_VALUE
------------
1
2
3
3 rows selected.
I know there are other ways but couldn't remember now, especially there is a way using XML query, if someone can post it.
Regards
Michel
|
|
|
| Re: Puzzle n°00 - Row generator * [message #291190 is a reply to message #291171 ] |
Thu, 03 January 2008 05:43   |
hobbes Messages: 173 Registered: January 2006 |
Senior Member |
|
|
This is on 10g.
SQL> define n=5
SQL> select to_number(column_value) colval
2 from xmltable('for $i in 1 to &n return $i');
COLVAL
----------
1
2
3
4
5
|
|
|
|
|
|
| Re: Puzzle n°00 - Row generator * [message #291223 is a reply to message #291216 ] |
Thu, 03 January 2008 07:42   |
Michel Cadot Messages: 17653 Registered: March 2007 Location: Nanterre, France, http://... |
Senior Member |
|
|
Ah ah! Ravujan is a little cheater! Copying and pasting the wiki.
Regards
Michel
|
|
|
| Re: Puzzle n°00 - Row generator * [message #291224 is a reply to message #291171 ] |
Thu, 03 January 2008 07:46   |
Michel Cadot Messages: 17653 Registered: March 2007 Location: Nanterre, France, http://... |
Senior Member |
|
|
Another MODEL query (>= 10g)
SQL> select val
2 from (select 1 rn from dual)
3 model
4 dimension by (rn)
5 measures (1 val)
6 rules
7 iterate (&N)
8 ( val[iteration_number] = iteration_number+1 )
9 order by val
10 /
VAL
----------
1
2
3
4
5
Regards
Michel
|
|
|
|
| Re: Puzzle n°00 - Row generator * [message #291355 is a reply to message #291327 ] |
Thu, 03 January 2008 23:55  |
Michel Cadot Messages: 17653 Registered: March 2007 Location: Nanterre, France, http://... |
Senior Member |
|
|
| Quote: | Now I feel I could paste the link instead
|
Yes, this was also my feeling not instead but in addition.
Without the link, it seems that you claimed it.
But you're right (in your other answer) this is a wrap-up.
Regards
Michel
|
|
|