CREATE TABLE employees
AS
SELECT * FROM hr.employees
Table created.
CREATE OR REPLACE PACKAGE my_arrays
IS
TYPE employees_t IS TABLE OF employees%ROWTYPE
INDEX BY PLS_INTEGER;
END;
Package created.
If You Can Do It in SQL....
DECLARE
l_employees my_arrays.employees_t;
BEGIN
DBMS_OUTPUT.PUT_LINE ('*********** Sort by Last Name');
/* Fill the collection from the table to
get things started. But just to be clear:
the more common use case for this ordering
will be with data what was pushed into the
collection from a PL/SQL-based algorithm -
not in any order or not easily sorted in your
code. */
SELECT *
BULK COLLECT INTO l_employees
FROM employees;
FOR emp IN ( SELECT *
FROM TABLE (l_employees)
WHERE department_id = 80
ORDER BY last_name)
LOOP
DBMS_OUTPUT.put_line (
emp.department_id || '-' || emp.last_name);
END LOOP;
DBMS_OUTPUT.PUT_LINE ('*********** Sort by Hire Date');
FOR emp IN ( SELECT *
FROM TABLE (l_employees)
WHERE last_name LIKE '%e%'
ORDER BY hire_date DESC)
LOOP
DBMS_OUTPUT.put_line (
TO_CHAR (emp.hire_date) || '-' || emp.last_name);
END LOOP;
END;
*********** Sort by Last Name
80-Abel
80-Ande
80-Banda
80-Bates
80-Bernstein
80-Bloom
80-Cambrault
80-Cambrault
80-Doran
80-Errazuriz
80-Fox
80-Greene
80-Hall
80-Hutton
80-Johnson
80-King
80-Kumar
80-Lee
80-Livingston
80-Marvins
80-McEwen
80-Olsen
80-Ozer
80-Partners
80-Russell
80-Sewall
80-Smith
80-Smith
80-Sully
80-Taylor
80-Tucker
80-Tuvault
80-Vishney
80-Zlotkey
*********** Sort by Hire Date
24-MAR-08-Ande
08-MAR-08-Markle
23-FEB-08-Lee
06-FEB-08-Philtanker
03-FEB-08-Geoni
29-JAN-08-Zlotkey
19-DEC-07-Perkins
12-DEC-07-Gee
10-AUG-07-Colmenares
21-JUN-07-OConnell
24-MAR-07-Bates
19-MAR-07-Greene
17-MAR-07-Jones
07-FEB-07-Lorentz
03-NOV-06-Sewall
28-SEP-06-Mikkilineni
26-AUG-06-Rogers
11-JUL-06-Gates
24-JUN-06-Dellinger
23-MAY-06-Feeney
06-APR-06-Patel
30-MAR-06-Olsen
23-FEB-06-Fleaur
12-FEB-06-Seo
11-NOV-05-Vishney
26-OCT-05-Stiles
28-SEP-05-Chen
16-JUL-05-Nayer
24-MAR-05-Bernstein
11-MAR-05-Ozer
03-MAR-05-Everett
30-JAN-05-Tucker
29-JAN-05-Davies
05-JAN-05-Partners
01-OCT-04-Russell
01-AUG-04-McEwen
18-JUL-04-Weiss
11-MAY-04-Abel
17-FEB-04-Hartstein
04-FEB-04-Bell
17-SEP-03-Whalen
07-DEC-02-Raphaely
17-AUG-02-Greenberg
16-AUG-02-Faviet
07-JUN-02-Gietz
07-JUN-02-Baer
13-JAN-01-De Haan
Replace Contents of Collection with New Order
DECLARE
l_employees my_arrays.employees_t;
l_employees2 my_arrays.employees_t;
BEGIN
DBMS_OUTPUT.PUT_LINE ('Sort by Last Name');
/* Fill the collection from the table to
get things started. */
SELECT *
BULK COLLECT INTO l_employees
FROM employees;
/* Re-order the contents of the collection
using SQL */
SELECT *
BULK COLLECT INTO l_employees2
FROM TABLE (l_employees)
ORDER BY last_name;
/* Iterate through collection by index value
to verify new order. */
FOR indx IN 1 .. l_employees2.COUNT
LOOP
DBMS_OUTPUT.put_line (l_employees2(indx).last_name);
END LOOP;
END;
Sort by Last Name
Abel
Ande
Atkinson
Austin
Baer
Baida
Banda
Bates
Bell
Bernstein
Bissot
Bloom
Bull
Cabrio
Cambrault
Cambrault
Chen
Chung
Colmenares
Davies
De Haan
Dellinger
Dilly
Doran
Ernst
Errazuriz
Everett
Faviet
Fay
Feeney
Fleaur
Fox
Fripp
Gates
Gee
Geoni
Gietz
Grant
Grant
Greenberg
Greene
Hall
Hartstein
Higgins
Himuro
Hunold
Hutton
Johnson
Jones
Kaufling
Khoo
King
King
Kochhar
Kumar
Ladwig
Landry
Lee
Livingston
Lorentz
Mallin
Markle
Marlow
Marvins
Matos
Mavris
McCain
McEwen
Mikkilineni
Mourgos
Nayer
OConnell
Olsen
Olson
Ozer
Partners
Pataballa
Patel
Perkins
Philtanker
Popp
Rajs
Raphaely
Rogers
Russell
Sarchand
Sciarra
Seo
Sewall
Smith
Smith
Stiles
Sullivan
Sully
Taylor
Taylor
Tobias
Tucker
Tuvault
Urman
Vargas
Vishney
Vollman
Walsh
Weiss
Whalen
Zlotkey