Distinct type assignments

The rules that apply to the assignments of distinct types to variables are different than the rules for all other assignments that involve distinct types.

Assignments to variables

The assignment of a distinct type to a variable is based on the source data type of the distinct type. Therefore, the value of a distinct type is assignable to a variable only if the source data type of the distinct type is assignable to the variable.

Example:

Assume that distinct type AGE was created with the following SQL statement:

   CREATE TYPE AGE AS SMALLINT WITH COMPARISONS

When the statement is executed, the following cast functions are also generated:

  AGE (SMALLINT) RETURNS AGE
  AGE (INTEGER) RETURNS AGE
  SMALLINT (AGE) RETURNS SMALLINT

Next, assume that column STU_AGE was defined in table STUDENTS with distinct type AGE. Now, consider this valid assignment of a student's age to host variable HV_AGE, which has an INTEGER data type:

   SELECT  STU_AGE INTO :HV_AGE FROM STUDENTS WHERE STU_NUMBER = 200

The distinct type value is assignable to the host variable HV_AGE because the source data type of the distinct type (SMALLINT) is assignable to the host variable (INTEGER).

Assignments other than to variables

A distinct type can be either the source or target of an assignment. Assignment is based on whether the data type of the value to be assigned is castable to the data type of the target. Casting between data types shows which casts are supported when a distinct type is involved. Therefore, a distinct type value can be assigned to any target other than a variable when:

  • the target of the assignment has the same distinct type, or
  • the distinct type is castable to the data type of the target.

Any value can be assigned to a distinct type when:

  • the value to be assigned has the same distinct type as the target, or
  • the data type of the assigned value is castable to the target distinct type.

Example:

Assume that the source data type for distinct type AGE is SMALLINT:

   CREATE TYPE AGE AS SMALLINT WITH COMPARISONS

Next, assume that two tables TABLE1 and TABLE2 were created with four identical column descriptions:

   AGECOL    AGE
   SMINTCOL  SMALLINT
   INTCOL    INTEGER
   DECCOL    DEC(6,2)  

Using the following SQL statement and substituting various values for X and Y to insert values into various columns of TABLE1 from TABLE2, Table 1 shows whether the assignments are valid.

   INSERT INTO  TABLE1 (Y) SELECT X FROM TABLE2
Table 1. Assessment of various assignments (for example on INSERT)
TABLE2.X TABLE1.Y Valid Reason
AGECOL AGECOL Yes Source and target are same distinct type
SMINTCOL AGECOL Yes SMALLINT can be cast to AGE (because AGE's source type is SMALLINT)
INTCOL AGECOL Yes INTEGER can be cast to AGE (because AGE's source type is SMALLINT)
DECCOL AGECOL No DECIMAL cannot be cast to AGE
AGECOL SMINTCOL Yes AGE can be cast to its source type SMALLINT
AGECOL INTCOL No AGE cannot be cast to INTEGER
AGECOL DECCOL No AGE cannot be cast to DECIMAL