VARRAY (Variable-size array) - Collection in PLSQL
In PL/SQL, a VARRAY (Variable-size array) is a type of collection that can store a fixed number of elements. Each element in the VARRAY is stored in a sequential manner, and all elements are of the same data type.
Characteristics of VARRAY:
Fixed Maximum Size: When you define a VARRAY, you specify the maximum number of elements it can hold. You cannot exceed this limit.
Sequential: Elements in a VARRAY are stored and accessed in a specific order, starting from index 1.
Homogeneous: All elements in a VARRAY must be of the same data type.
Densely Indexed: A VARRAY is always densely populated. This means that there are no gaps between indexes.
Stored in Oracle Database: When used in database columns, VARRAYs are stored inline with the table row data.
Syntax for VARRAY in PL/SQL
- Declaring a VARRAY Type
You first define a VARRAY type, specifying the number of elements it can hold and the data type of the elements.
TYPE varray_name IS VARRAY(max_size) OF element_type;
varray_name: Name of the VARRAY type.
max_size: Maximum number of elements that can be stored in the VARRAY.
element_type: Data type of the elements stored in the VARRAY.
- Defining and Initializing a VARRAY
Once the VARRAY type is declared, you can declare variables of that type and initialize them with values.
DECLARE
TYPE employee_varray IS VARRAY(5) OF VARCHAR2(30); -- VARRAY type declaration with a maximum size of 5
employee_names employee_varray := employee_varray('John', 'Jane'); -- Initializing with 2 elements
BEGIN
-- Statements to use the VARRAY
END;
Operations on VARRAYs:
Here are some common operations you can perform on VARRAYs:
EXTEND(n): Adds n elements to the end of the VARRAY.
COUNT: Returns the current number of elements in the VARRAY.
TRIM(n): Removes n elements from the end of the VARRAY.
FIRST and LAST: Returns the first and last index in the VARRAY.
Example: Declaring and Using a VARRAY
DECLARE
-- Declare a VARRAY type that can hold up to 5 elements of VARCHAR2(50)
TYPE employee_varray IS VARRAY(5) OF VARCHAR2(50);
-- Declare a variable of this VARRAY type and initialize it with two values employee_names employee_varray := employee_varray('John', 'Jane');
BEGIN
-- Add more elements to the VARRAY
employee_names.EXTEND(3); -- Extend the array by 3 more elements
employee_names(3) := 'Sam';
employee_names(4) := 'Peter';
employee_names(5) := 'Lucy';
-- Print all the names in the VARRAY FOR i IN 1..employee_names.COUNT LOOP DBMS_OUTPUT.PUT_LINE('Employee ' || i || ': ' || employee_names(i)); END LOOP; -- Trim 1 element from the end of the VARRAY employee_names.TRIM(1); DBMS_OUTPUT.PUT_LINE('After trimming 1 element:'); -- Print remaining names in the VARRAY FOR i IN 1..employee_names.COUNT LOOP DBMS_OUTPUT.PUT_LINE('Employee ' || i || ': ' || employee_names(i)); END LOOP;
END;
/
Explanation:
VARRAY Type Declaration: We declare a VARRAY type employee_varray that can hold up to 5 elements of type VARCHAR2(50).
Initialization: We initialize the employee_names variable with two names: 'John' and 'Jane'.
EXTEND: We use the EXTEND(3) method to extend the VARRAY to hold 3 more elements, making room for a total of 5 elements.
Accessing Elements: We assign values to the new positions (3 to 5) and print all elements using a loop.
TRIM: The TRIM(1) method is used to remove the last element from the VARRAY, reducing the number of elements from 5 to 4.
Output:
Employee 1: John
Employee 2: Jane
Employee 3: Sam
Employee 4: Peter
Employee 5: Lucy
After trimming 1 element:
Employee 1: John
Employee 2: Jane
Employee 3: Sam
Employee 4: Peter
Key Points:
VARRAYs are useful when you know the maximum size of your collection ahead of time.
Sequential Access: Elements in VARRAYs are accessed using their index.
Extending and Trimming: You can dynamically add or remove elements from the VARRAY using EXTEND and TRIM.
The above is the detailed content of VARRAY (Variable-size array) - Collection in PLSQL. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
