Varray and bulk collect

Varray and bulk collect in PL/SQL

In PL/SQL, a VARRAY (variable-size array) is a custom data type that allows you to store a fixed number of elements of the same data type. VARRAYs are similar to arrays in other programming languages, but they have some differences in how they are defined and used.

Here is an example of how to define a VARRAY type:

CREATE TYPE number_varray AS VARRAY(10) OF NUMBER;

In this example, we are creating a VARRAY type called “number_varray” that can hold up to 10 elements of type NUMBER.

You can declare a variable of this type and use it in your PL/SQL code, like this:

DECLARE
  v_numbers number_varray := number_varray(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
BEGIN
  -- do something with v_numbers
END;

VARRAYs are useful when you need to work with a fixed number of elements of the same data type, but they can be inefficient when working with large datasets. This is where bulk collect comes in.

Bulk collect is a feature of PL/SQL that allows you to fetch multiple rows of data at once into a collection, instead of one row at a time. This can significantly improve performance when working with large datasets. Here is an example of how to use bulk collect:

DECLARE
  TYPE emp_tab IS TABLE OF employees%ROWTYPE;
  l_emps emp_tab;
BEGIN
  SELECT *
  BULK COLLECT INTO l_emps
  FROM employees;
  -- do something with l_emps
END;

In this example, we are selecting all rows from the “employees” table and using bulk collect to fetch them into a collection called “l_emps”. We can then use this collection to perform operations on the data, such as updating or inserting multiple rows at once.

Overall, VARRAYs and bulk collect are two important features of PL/SQL that can help you work with data efficiently and effectively.

Apply for PL/SQL Certification

https://www.vskills.in/certification/certified-pl-sql-developer

Back to Tutorials

Share this post
[social_warfare]
Custom data types
Using layers

Get industry recognized certification – Contact us

keyboard_arrow_up