Oracle internals, debugging and undocumented features

Querying multiple rows from dual

By Granville Bonyata

Sometimes it’s useful to have a query that always returns a certain number of rows, such as a report that returns one row for every day of the month. Using DUAL and CONNECT BY allows you to return as many rows as needed. For instance, this query returns one row for every day of the month:

SELECT TO_DATE(level||’-‘||TO_CHAR(SYSDATE,’MON-YYYY’)) each_day from dual connect by level <= TO_CHAR(LAST_DAY(SYSDATE),’DD’) ORDER BY 1;

SQL> /

EACH_DAY                                                                        ———                                                                       01-JAN-12                                                                       02-JAN-12                                                                       03-JAN-12                                                                       04-JAN-12                                                                       05-JAN-12                                                                       06-JAN-12                                                                       07-JAN-12                                                                       08-JAN-12                                                                       09-JAN-12                                                                       10-JAN-12                                                                       11-JAN-12                                                                       12-JAN-12                                                                       13-JAN-12                                                                       14-JAN-12                                                                       15-JAN-12                                                                       16-JAN-12                                                                       17-JAN-12                                                                       18-JAN-12                                                                       19-JAN-12                                                                       20-JAN-12                                                                       21-JAN-12                                                                       22-JAN-12                                                                       23-JAN-12                                                                       24-JAN-12                                                                       25-JAN-12                                                                       26-JAN-12                                                                       27-JAN-12                                                                       28-JAN-12                                                                       29-JAN-12                                                                       30-JAN-12                                                                       31-JAN-12

31 rows selected.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: