Reading:  

To the point guide to PL/SQL


Working with Arrays

PL/SQL provides a data structure called Varrays is single-dimensional, where can store a same type of elements for fixed length. The arrays variable maximum length defined at the time of declaration.

Points of varrays:

  • Fixed length.
  • Elements of Arrays of the same type.

All varrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

 

   img source: oracle.com

 

Creating a Varray Type:

Size is mandatory in creating varray variable

declare
type VarrayType is varray(size) of ElementType;
...
create or replace type VarrayType is varray(size) of ElementType;

Let's now check some of the details from above syntax

  • varray_type_name is a variable name,
  • size: number of elements in the varray,
  • element_type: data type of the data of the array.

 

CREATE Or REPLACE TYPE studname AS VARRAY(5) OF VARCHAR2(20);
/
Type created.

The syntax for creating a VRRAY type:

TYPE varray_type_name IS VARRAY(n) of <element_type>

Example

Type rank IS VARRAY(3) OF INTEGER;

Example

DECLARE
   type studname IS VARRAY(3) OF VARCHAR2(20);
   type rank IS VARRAY(3) OF INTEGER;
   names studname;
   score rank;
  BEGIN
   names := studname ('Pooja', 'Ravi', 'Rathan');
   score:= rank (2, 1, 3);
      FOR x in 1.. 3 LOOP
      dbms_output.put_line('Detail of Student: ' || names (x) || '
      Rank: ' || score (x));
   END LOOP;
END;
/
 

 

When you execute above code you will get

Detail of Student: Pooja Rank: 2
Detail of Student: Ravi Rank: 1
Detail of Student: Rathan Rank: 3
PL/SQL procedure successfully completed.

In next part of this guide we will check on what procedures are and how we use them.

Description

This tutorial focus on PL/SQL and covers the below topics

  • What is PL/SQL?
  • Environment Setup
  • Variables
  • Data Types 
  • Constants 
  • Operators
  • Conditions
  • Loops
  • Strings
  • Arrays
  • Procedures
  • Functions
  • Cursors
  • Records
  • Exceptions
  • Packages
  • Triggers
  • Collections
  • Transactions
  • Date & Time
  • Object Oriented
  • DBMS Output

If you found any error with any of the docs please let us know.

 



Learning Objectives

Learn PL/SQL from a beginners perspective, this guide can also help you if you are trying to brush up your PLSQL skills

Author: Subject Coach
Added on: 20th Apr 2015

You must be logged in as Student to ask a Question.

None just yet!