PL-SQL Tables
PL-SQL Tables
COM
Introduction
Pl/SQL Tables
Characteristics of PL/SQL Tables
Using DML Statements with PL/SQL Tables
How to declare a PL/SQL table
PL/SQL Tables
PL/SQL table are only available with PL/SQL version 2 and greater. PL/SQL tables reside in the
Private Global area (PGA) on the Oracle database instance. They only exist as a server side
structure and as such it is not possible to manipulate PL/SQL tables in the Oracle Developer
2000 environment.
PL/SQL tables are an enigma, they are like an array but they are not, they are also like a table
but they are not!
I will explain the characteristics of PL/SQL tables and arrays and SQL tables, so that you will
understand the differences so you will know how and when to use PL/SQL in your programs.
They are one dimensional, a PL/SQL table can only have one column, like a one dimensional
array. Unlike arrays that can be defined with more than one dimension.!
They are unbounded. What this means is that there is no limit to the number of rows in a
PL/SQL table. The PL/SQL table grows dynamically as you add more rows to the table. Unlike an
array that must be defined at creation!
They are sparse. This means that a row exists in the table only when a value has been
assigned to a row. Additionally rows do not need to be assigned sequentially. We can assign
row 10, 30 then rows 10231. With no values assigned between! Unlike an array, which is
regarded as a dense structure. When an array is defined, all the cells in the array are assigned
memory.
They are homogenous. Because they can only have a single column, all the rows in the PL/SQL
table contain the same datatype. Hence homogenous.
Note: With the introduction of PL/SQL Version 2.3 you can have PL/SQL tables of records. The
resulting table is still homogenous. Each row contains the same set of columns.
PL/SQL tables have a single indexing mode, called BINARY_INTEGER. This acts as the primary
key of the PL/SQL table. As the index is an integer, it is not possible to have a row defined as
table(23.3) but table(-23) is possible.
You cannot SELECT from a PL/SQL table, you must use PL/SQL loops to move through the table
one row at a time.
You must define the PL/SQL table structure, e.g. NUMBER, VARCHAR2 etc. using the table TYPE
statement.
Declare the table based on that table type. The declaration of a PL/SQL table is a specific
instance of a table type.
Code:
TYPE names IS TABLE OF varchar2 [NOT NULL]
INDEX BY BINARY_INTEGER
This has defined a table type called names consisting of the datatype varchar2. The NOT NULL
is optional.
The datatypes used in the table type can be one of the following.
Code:
<table_name> <table_type>;
Code:
customer_names names;
This has declared a PL/SQL table called customer_names based on the names structure
defined earlier.