Examples of PL/SQL Nested Table

Example 1: Accessing Elements by Their Indexes

Indexes in PL/SQL nested tables start from 1. Elements are accessed by specifying the index.

Syntax:

SET SERVEROUTPUT ON;

DECLARE

— DECALRE NESTED TABLE AND THE VARIABLE

BEGIN

DBMS_OUTPUT.PUTLINE(nested_table_variable(1 ));

END;

The first element of the nested table is printed by specifying the index with the variable.

After putting all the mentioned things together we can perform operation on nested table.

Query:

SET SERVEROUTPUT ON;
DECLARE
TYPE First_nested_table IS TABLE OF NUMBER;
example_one First_nested_table;
BEGIN
-- Initialize the nested table
example_one := First_nested_table(1, 2, 3, 4, 5);
FOR i IN example_one.FIRST .. example_one.LAST LOOP
DBMS_OUTPUT.PUT_LINE('Element ' || i || ': ' || example_one(i));
END LOOP;
-- Free up memory
example_one := NULL;
END;

Output:

Nested Table in PLSQL

Explanation:

The output confirms the successful initialization of the nested table with numeric elements, the correct iteration over its elements, and the subsequent freeing up of memory. Each element is displayed with its corresponding index in the output.

Example 2: Initializing and Printing Elements of a Nested Table in PL/SQL

Query

SET SERVEROUTPUT ON;
DECLARE
TYPE First_nested_table IS TABLE OF VARCHAR2(20);
example_one First_nested_table;
BEGIN
-- Initialize the nested table
example_one := First_nested_table('ONE','TWO','THREE','FOUR');
FOR i IN example_one.FIRST .. example_one.LAST LOOP
DBMS_OUTPUT.PUT_LINE('Element ' || i || ': ' || example_one(i));
END LOOP;
-- Free up memory
example_one := NULL;
END;
/

Output:

Nested TAbles in PLSQL

Explanation:

The output confirms the successful initialization of the nested table, the correct iteration over its elements, and the subsequent freeing up of memory. Each element is displayed with its corresponding index in the output.

Example 3: Addition of the Elements of the Nested Tables

Query:

SET SERVEROUTPUT ON;
DECLARE
TYPE NST_TBL IS TABLE OF NUMBER;
-- Declare two nested tables
var1 NST_TBL := NST_TBL(1, 2);
var2 NST_TBL := NST_TBL(3, 4);
-- result nested table
result NST_TBL;
BEGIN
result := NST_TBL();
result.EXTEND(var1.COUNT);
--addition of the elements
FOR i IN 1..var1.LAST LOOP
result(i) := var1(i) + var2(i);
END LOOP;
-- Display result of addition
DBMS_OUTPUT.PUT_LINE('Result(1): ' || result(1) || ' Result(2): ' || result(2));
END;
/

Output:

PLSQL NESTED Tables

Output:

Elements of the two nested table are added and are stored in new nested table.Element is added with element of the same index.

PL/SQL Nested Table

PL/SQL stands for Procedural Language/ Structured Query Language. It has block structure programming features.PL/SQL supports SQL queries. It also supports the declaration of the variables, control statements, Functions, Records, Cursor, Procedure, and Triggers.PL/SQL contains a declaration section, execution section, and exception-handling section. Declare and exception handling sections are optional. Every PL/SQL query contains BEGIN, and END keywords. We can nest blocks in PL/SQL. It supports anonymous blocks and named blocks. Anonymous blocks are not stored in the database while named blocks are stored.

In this article, we’ll explore nested tables in PL/SQL. We’ll look at what they are, how they work, and what they can be used for.PL/SQL is a procedural version of Oracle’s SQL. It’s designed to support complex data structures. Nested tables are a great example of this.

Syntax of PL/SQL block:

Declaration section

BEGIN

Execution section

EXCEPTION

Exception section

END;

Similar Reads

PL/SQL Nested Tables

PL/SQL supports a nested table. It is a type of data structure like an array in a programming language. Multiple values are stored in a single column. They are stored in a database. Nested tables in PL/SQL are dynamic i.e. the amount of memory utilized can increase or decrease as per use. They allow you to work on variable amounts of data dynamically. They are used when an unknown number of elements are to be stored. Elements of any datatype can be stored in a nested table....

Examples of PL/SQL Nested Table

Example 1: Accessing Elements by Their Indexes...

Conclusion

In conclusion, PL/SQL is a procedural version of Oracle’s SQL. Nested Tables is dynamic arrays, they can grow or shrink as per the scenario. It is used to manage complex data. Variables of nested table type are initialized using the constructor. Elements from the nested tables are accessed using an index. In this article, we have learned about the nested tables in pl/sql....

Contact Us