How to Loop to Populate an Array Object in PL/SQL

An array object in PL/SQL offers similar data storage and access as a regular database table. Unlike a database table, an array object is designed for temporary storage, allowing database administrators to decrease the workload of a database server. PL/SQL, also known as Programming Language SQL, incorporates the functionality of programming languages directly into standard SQL commands, allowing database administrators to run programming operations such as loops resorting to a separate language. Using loops in PL/SQL is a convenient way to populate an array object.

Instructions

    • 1

      Create the array object. Declare the name of the array and how PL/SQL should organize the array. For example, "DECLARE TYPE employee_table IS TABLE OF varchar2(50) INDEX BY binary_integer" creates an array named "employee_table" that accepts letters as entries and is sorted by a binary integer.

    • 2

      Add fields to the array object. Include the array index variable "i" that identifies each entry. For example, use the code "employee employee_table; i binary_integer" to add the fields "employee" and "i."

    • 3

      Start the loop. Type the command "Begin." Set the index variable "i" to the first cell you want the loop to populate. For example, "Begin i:=1;" starts the loop entering into "cell 1."

    • 4

      Enter the command for the loop. Use the commands and syntax of PL/SQL to code the operation you wish repeated. For example, "for emp_rec in (select last_name from employees) loop employee(i):=emp_rec.last_name; i:=i + 1; end loop; end;" populates an array with the values "last_name" from the "employees" table.

Learnify Hub © www.0685.com All Rights Reserved