This script can be used to return a range of rows from the database.
Back to SQL scripts
| SELECT | b.* |
| FROM |
| | (SELECT rownum rnum, emp.* |
| | FROM emp) b |
| WHERE | b.rnum between &initial and &final; |
To run the code, cut and paste it onto your favourite editor and save the file.
When you run it through SQL*PLUS, it asks you to specify the range
of the rows you want to see. So for example if you want to display all rows
between the 2nd and the 6th row, both inclusive, enter the value 2
for &initial and 6 for &final, when prompted.
When used with the emp table, as in the example, you get the
following output.
| SQL> | SELECT * |
| | FROM |
| | (SELECT rownum rnum, emp.* |
| | FROM emp) b |
| | WHERE b.rnum between &initial and &final; |
| Enter | value for initial: 2 |
| Enter | value for final: 5 |
| old 5: | WHERE b.rnum between &initial and &final |
| new 5: | WHERE b.rnum between 2 and 5 |
| RNUM | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
| --------- | --------- | ---------- | --------- | --------- | --------- | --------- | --------- | --------- |
| 2 | 7499 | ALLEN | SALESMAN | 7698 | 20-FEB-81 | 1600 | 300 | 30 |
| 3 | 7521 | WARD | SALESMAN | 7698 | 22-FEB-81 | 1250 | 500 | 30 |
| 4 | 7566 | JONES | MANAGER | 7839 | 02-APR-81 | 2975 | | 20 |
| 5 | 7654 | MARTIN | SALESMAN | 7698 | 28-SEP-81 | 1250 | 1400 | 30 |
SQL>
Back to SQL scripts