Preamble
In Oracle PL/SQL, the PRIOR and NEXT methods are functions that allow you to move back and forth in the collection (ignoring the deleted items even if DELETE stores fillers for them). These methods are useful for crossing rare collections.
Considering the index:
- PRIOR returns the index of the previous existing element in the collection, if any. Otherwise, PRIOR returns NULL.
- For any collection c, c.PRIOR (c.FIRST) returns NULL.
- NEXT returns the index of a subsequent existing collection item, if one exists. Otherwise, NEXT returns NULL.
- For any c collection, c.NEXT (c.LAST) returns NULL.
- This index does not have to exist. However, if the collection is Varray, the index cannot exceed LIMIT.
Syntax of PRIOR and NEXT collection methods in Oracle PL/SQL
collection_name.PRIOR;
collection_name.NEXT;
Parameters and arguments of PRIOR and NEXT collection methods
- collection_name – the name of the collection.
- PRIOR – the previous existing element of the collection.
- NEXT – the next existing element of the collection.
See also collection methods: DELETE, TRIM, EXTEND, EXISTS, FIRST and LAST, COUNT, LIMIT.
An example to understand how to use the PRIOR and NEXT collection methods in Oracle PL/SQL
An example that initializes a Nested Table with six elements, removes the fourth element and then shows the PRIOR and NEXT values for elements 1 to 7. Elements 4 and 7 do not exist. Element 2 exists despite its zero value.
DECLARE
TYPE nt_type IS TABLE OF NUMBER;
nt nt_type := nt_type(18, NULL, 36, 45, 54, 63);
BEGIN
nt.DELETE(4);
DBMS_OUTPUT.PUT_LINE('nt(4) was deleted.');
FOR i IN 1...7 LOOP
DBMS_OUTPUT.PUT('nt.PRIOR(' || i || ') = '); print(nt.PRIOR(i));
DBMS_OUTPUT.PUT('nt.NEXT(' || i || ') = '); print(nt.NEXT(i));
END LOOP;
END LOOP;
Result:
nt(4) was deleted.
nt.PRIOR(1) = NULL
nt.NEXT(1) = 2
nt.PRIOR(2) = 1
nt.NEXT(2) = 3
nt.PRIOR(3) = 2
nt.NEXT(3) = 5
nt.PRIOR(4) = 3
nt.NEXT(4) = 5
nt.PRIOR(5) = 3
nt.NEXT(5) = 6
nt.PRIOR(6) = 5
nt.NEXT(6) = NULL
nt.PRIOR(7) = 6
nt.NEXT(7) = NULL
For an Associative Array indexed by a string, the previous and next indexes are determined by the key values, which are in sorted order. In the example below, FIRST, NEXT and WHILE LOOP loop are used to print Associative Array elements.
DECLARE
TYPE NumList IS TABLE OF NUMBER;
n NumList := NumList(1, 2, NULL, NULL, 5, NULL, 7, 8, 9, NULL);
subscript INTEGER;
BEGIN
DBMS_OUTPUT.PUT_LINE('First to last:');
subscript := n.FIRST;
WHILE subscript IS NOT NULL LOOP
DBMS_OUTPUT.PUT('n(' || subscript || ') = ');
print(n(subscript));
subscript := n.NEXT(subscript);
END LOOP;
DBMS_OUTPUT.PUT_LINE('--------------');
DBMS_OUTPUT.PUT_LINE('Last to first:');
subscript := n.LAST;
WHILE subscript IS NOT NULL LOOP
DBMS_OUTPUT.PUT('n(' || subscript || ') = ');
print(n(subscript));
subscript := n.PRIOR(subscript);
END LOOP;
END;
Result:
First to last:
n(1) = 1
n(2) = 2
n(3) = NULL
n(4) = NULL
n(5) = 5
n(6) = NULL
n(7) = 7
n(8) = 8
n(9) = 9
n(10) = NULL
--------------
Last to first:
n(10) = NULL
n(9) = 9
n(8) = 8
n(7) = 7
n(6) = NULL
n(5) = 5
n(4) = NULL
n(3) = NULL
n(2) = 2
n(1) = 1
This example prints the elements of an unlimited Nested Table from first to last using FIRST and NEXT, and from last to first using LAST and PRIOR.
PL/SQL tutorial: PL/SQL Collection Method Prior & Next in Oracle Database
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of clouds, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Open Banking APIs: Where Performance = Trust
- 30 October 2025
- Software Engineering
Introduction Open banking promised to be a paradigm shift — enabling consumers to share financial data securely and allowing banks, fintechs, and third parties to build innovative services on that foundation. But as the ecosystem evolves, one truth stands out: it’s not just about access — it’s about performance. An open banking API that’s slow, … Continue reading “Open Banking APIs: Where Performance = Trust”
Enteros for the Travel Industry: Enhancing Cost Estimation Accuracy Through AIOps, Observability, and Cloud FinOps
Introduction In the fast-moving world of travel and hospitality, accurate cost estimation isn’t just a finance issue—it’s a performance challenge. From dynamic booking systems and real-time analytics to backend inventory databases and AI-driven recommendation engines, every operational layer relies on complex data interactions. The travel industry has always faced volatile demand, fluctuating operating costs, and … Continue reading “Enteros for the Travel Industry: Enhancing Cost Estimation Accuracy Through AIOps, Observability, and Cloud FinOps”
Transforming Data Lake Efficiency in the Technology Sector: How Enteros’ AI Performance Platform Redefines Database Optimization
Introduction In today’s data-driven technology landscape, the backbone of innovation lies in how efficiently enterprises manage and utilize their data. With the rise of big data, cloud ecosystems, and AI workloads, data lakes have become the central hub of data intelligence—storing massive volumes of structured, semi-structured, and unstructured data. However, as organizations scale their digital … Continue reading “Transforming Data Lake Efficiency in the Technology Sector: How Enteros’ AI Performance Platform Redefines Database Optimization”
Redefining Healthcare Efficiency: AI-Driven Backlog Prioritization and Capital Expenditure Optimization with Enteros
- 29 October 2025
- Database Performance Management
Introduction The healthcare industry is under constant pressure to balance two competing priorities — improving patient outcomes and managing operational efficiency within constrained budgets. With digital transformation accelerating across hospitals, clinics, and research institutions, vast amounts of data are being generated from electronic health records (EHRs), diagnostic imaging, clinical workflows, and administrative systems. This influx … Continue reading “Redefining Healthcare Efficiency: AI-Driven Backlog Prioritization and Capital Expenditure Optimization with Enteros”