DEV Community

Pranav Bakare
Pranav Bakare

Posted on

RECORD in PLSQL

RECORD in PLSQL

In PL/SQL, defining a RECORD involves creating a custom data type that can hold a collection of fields, similar to a row in a table. After defining the RECORD type, you can declare a variable of this type to use it in your PL/SQL block. Here's how you can do both:

1. Define a RECORD Type

To define a RECORD, use the TYPE keyword to create a composite data structure with multiple fields, where each field can have a different data type.

Syntax:

TYPE record_type IS RECORD (
    field1 datatype,
    field2 datatype,
    ...
    fieldN datatype
);
Enter fullscreen mode Exit fullscreen mode

Example:


DECLARE
    -- Defining a RECORD type to store employee information
    TYPE emp_record IS RECORD (
        emp_id NUMBER,
        emp_name VARCHAR2(50),
        emp_salary NUMBER
    );
Enter fullscreen mode Exit fullscreen mode

2. Declare a Variable Using the RECORD Type

After defining the RECORD type, you can declare a variable of that type to store data.

Syntax:

record_variable record_type;
Enter fullscreen mode Exit fullscreen mode

Example:


DECLARE
    -- Defining the RECORD type
    TYPE emp_record IS RECORD (
        emp_id NUMBER,
        emp_name VARCHAR2(50),
        emp_salary NUMBER
    );

    -- Declaring a variable of the RECORD type
    employee emp_record;
BEGIN
    -- Assigning values to the fields of the RECORD variable
    employee.emp_id := 101;
    employee.emp_name := 'John Doe';
    employee.emp_salary := 5000;

    -- Printing the values
    DBMS_OUTPUT.PUT_LINE('Employee ID: ' || employee.emp_id);
    DBMS_OUTPUT.PUT_LINE('Employee Name: ' || employee.emp_name);
    DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || employee.emp_salary);
END;
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Define the Record: The TYPE statement is used to create a RECORD type called emp_record, with fields for emp_id, emp_name, and emp_salary.

  2. Define the Variable in Record: The variable employee of type emp_record is then declared, and each field in employee can store individual data values.

This approach allows you to group related information together, making it easier to manage and manipulate data in PL/SQL programs.


RECORD in PLSQL

In PL/SQL, a RECORD is designed to store a single row of data, not multiple rows. This is because a RECORD is a composite data type that groups multiple related fields (like columns in a table) into a single unit. However, it can only hold one set of these fields at a time (one "row").

Why Can't a RECORD Hold Multiple Records?

A RECORD type in PL/SQL is essentially a structure that represents a single unit of data. It's like a single row in a database table where each field represents a column. Since it's a single composite data type, it can't inherently handle multiple rows or instances of data.


Alternative to Handle Multiple Records

To handle multiple records, PL/SQL provides collections. You can use collections to create arrays or tables of RECORDs, allowing you to work with multiple rows of data. The three main types of collections are:

  1. Associative Arrays (Index-By Tables)

  2. Nested Tables

  3. VARRAYs (Variable-Size Arrays)

Each of these can be used to store multiple RECORDs, effectively creating a way to handle multiple rows.

Top comments (0)