Oracle PL/SQL - Conversion between constrained subtype and base type

Introduction

A constrained subtype can be implicitly converted to its base type.

The base type can be implicitly converted to the constrained subtype only if the value does not violate a constraint of the subtype.

A constrained subtype can be implicitly converted to another constrained subtype with the same base type only if the source value does not violate a constraint of the target subtype.

In the following code, the three constrained subtypes have the same base type.

The first two subtypes can be implicitly converted to the third subtype, but not to each other.

Demo

SQL> --Implicit Conversion Between Constrained Subtypes with Same Base Type
SQL> DECLARE
  2    SUBTYPE type_one    IS PLS_INTEGER RANGE 0..9;
  3    SUBTYPE type_two    IS PLS_INTEGER RANGE 10..99;
  4    SUBTYPE type_three  IS PLS_INTEGER RANGE 0..99;
  5-- w w  w  .j a v  a 2 s. co  m
  6    d   type_one        :=  4;
  7    dd  type_two := 35;
 11    u   type_three;
 12  BEGIN
 13    u := d;   
 14    u := dd; 
 15    dd := d;  -- Raises error; type_two range does not include type_one range
 16  END;
 17  /

SQL>

Related Topic