Oracle PL/SQL - Declaring Record Constants

Introduction

To declare a record constant, create a function that populates the record with its initial value.

Then invoke the function in the constant declaration.

Demo

SQL>
SQL>-- from  w  w  w  .j  av  a2 s .  c  o  m
SQL> CREATE OR REPLACE PACKAGE My_Types AUTHID DEFINER IS
  2    TYPE My_Rec IS RECORD (a NUMBER, b NUMBER);
  3    FUNCTION Init_My_Rec RETURN My_Rec;
  4  END My_Types;
  5  /

Package created.

SQL> CREATE OR REPLACE PACKAGE BODY My_Types IS
  2    FUNCTION Init_My_Rec RETURN My_Rec IS
  3      Rec My_Rec;
  4    BEGIN
  5      Rec.a := 0;
  6      Rec.b := 1;
  7      RETURN Rec;
  8    END Init_My_Rec;
  9  END My_Types;
 10  /

Package body created.

SQL> DECLARE
  2    r CONSTANT My_Types.My_Rec := My_Types.Init_My_Rec();
  3  BEGIN
  4    DBMS_OUTPUT.PUT_LINE('r.a = ' || r.a);
  5    DBMS_OUTPUT.PUT_LINE('r.b = ' || r.b);
  6  END;
  7  /
r.a = 0
r.b = 1

PL/SQL procedure successfully completed.

SQL>

Related Topic