Using %TYPE in variable declarations v17
Often, variables are declared in SPL programs that are used to hold values from tables in the database. To ensure compatibility between the table columns and the SPL variables, make sure their data types are the same.
However, often a change is made to the table definition. If the data type of the column is changed, you might need to make the corresponding change to the variable in the SPL program.
Instead of coding the specific column data type into the variable declaration, you can use the column attribute %TYPE
. Specify a qualified column name in dot notation or the name of a previously declared variable as a prefix to %TYPE
. The data type of the column or variable prefixed to %TYPE
is assigned to the variable being declared. If the data type of the given column or variable changes, the new data type is associated with the variable, and you don't need to modify the declaration code.
Note
You can use the %TYPE
attribute with formal parameter declarations as well.
Syntax
name
is the identifier assigned to the variable or formal parameter that's being declared.column
is the name of a column intable
orview
.variable
is the name of a variable that was declared prior to the variable identified byname
.
Note
The variable doesn't inherit any of the column’s other attributes that you specify on the column with the NOT NULL
clause or the DEFAULT
clause.
Example: Defining parameters using %TYPE
In this example, a procedure:
- Queries the
emp
table using an employee number - Displays the employee’s data
- Finds the average salary of all employees in the department to which the employee belongs
- Compares the chosen employee’s salary with the department average
Alternatively, you can write the procedure without explicitly coding the emp
table data types into the declaration section of the procedure:
p_empno
shows an example of a formal parameter defined using %TYPE
. v_avgsal
shows the use of %TYPE
referring to another variable instead of a table column.
The following is sample output from executing this procedure:
- On this page
- Syntax
- Example: Defining parameters using %TYPE