Qualified expressions for PL/SQL Oracle Database 18C

In Oracle Database 18C type initialization is simplified with qualified expressions for Record type and Associative Array Type. It can help simplify coding.

Record

rec_var := (field_name => some_value, .., field_name => some_value)

Associative Array

rec_var := (1 => some_value, .., 10 => some_value)

RECORD type example

DECLARE
  TYPE l_rec_type IS RECORD
  (
    username  VARCHAR2(30),
    phone     VARCHAR2(30),
    town      VARCHAR2(30)
  );
  
  l_user l_rec_type;
BEGIN
  --works for version 12C
  l_user := l_rec_type('Tomasz', NULL, 'Warsaw');

  --new 18C
  l_user := l_rec_type
            (
              username => 'Tomasz', 
              phone    => 'Warsaw'
            );
END;
/

ASSOCIATIVE ARRAY example

DECLARE
  TYPE l_rec_idx_type IS TABLE OF VARCHAR(30) INDEX BY BINARY_INTEGER;
  TYPE l_rec_var_type IS TABLE OF VARCHAR(30) INDEX BY VARCHAR2(30);
  
  l_idx l_rec_idx_type;
  l_var l_rec_var_type;
BEGIN
  --works for version 12C
  l_idx(1) := 'Tomasz';
  l_idx(2) := 'Adam';
  l_idx(4) := 'Eve';

  --new 18C
  l_idx := l_rec_idx_type
           (
             1 => 'Tomasz', 
             2 => 'Adam', 
             4 => 'Eve'
           );
  
  --works for version 18C 
  l_var('Tomasz') := 'Lesinski';
  l_var('Adam')   := 'First';
  l_var('Eve')    := 'Second';
  
  --new 18C
  l_var := l_rec_var_type
           (
             'Tomasz' => 'Lesinski', 
             'Adam'   => 'First', 
             'Eve'    => 'Second'
           );
END;
/

Have a fun 🙂

Tomasz

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.