Home Web Front-end JS Tutorial VARRAY (Variable-size array) - Collection in PLSQL

VARRAY (Variable-size array) - Collection in PLSQL

Oct 03, 2024 am 06:23 AM

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:

  1. Fixed Maximum Size: When you define a VARRAY, you specify the maximum number of elements it can hold. You cannot exceed this limit.

  2. Sequential: Elements in a VARRAY are stored and accessed in a specific order, starting from index 1.

  3. Homogeneous: All elements in a VARRAY must be of the same data type.

  4. Densely Indexed: A VARRAY is always densely populated. This means that there are no gaps between indexes.

  5. Stored in Oracle Database: When used in database columns, VARRAYs are stored inline with the table row data.

Syntax for VARRAY in PL/SQL

  1. 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.

  1. 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');
Copy after login

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;
Copy after login

END;
/

Explanation:

  1. VARRAY Type Declaration: We declare a VARRAY type employee_varray that can hold up to 5 elements of type VARCHAR2(50).

  2. Initialization: We initialize the employee_names variable with two names: 'John' and 'Jane'.

  3. EXTEND: We use the EXTEND(3) method to extend the VARRAY to hold 3 more elements, making room for a total of 5 elements.

  4. Accessing Elements: We assign values to the new positions (3 to 5) and print all elements using a loop.

  5. 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

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...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

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.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

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 using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

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...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

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.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

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 Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

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.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

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. �...

See all articles