%TYPE Attribute

The %TYPE attribute lets use the datatype of a field, record, nested table, database column, or variable in your own declarations, rather than hardcoding the type names. You can use the %TYPE attribute as a datatype specifier when declaring constants, variables, fields, and parameters. If the types that you reference change, your declarations are automatically updated. This technique saves you from making code changes when, for example, the length of a VARCHAR2 column is increased. Note that column constraints, such as the NOT NULL and check constraint, or default values are not inherited by items declared using %TYPE. For more information, see "Using the %TYPE Attribute".


%type attribute ::=

Description of type_attribute.gif follows
Description of the illustration type_attribute.gif

Keyword and Parameter Description


A nested table, index-by table, or varray previously declared within the current scope.


A PL/SQL cursor variable previously declared within the current scope. Only the value of another cursor variable can be assigned to a cursor variable.


A table and column that must be accessible when the declaration is elaborated.


An instance of an object type, previously declared within the current scope.


A user-defined or %ROWTYPE record, previously declared within the current scope.


A field in a user-defined or %ROWTYPE record, previously declared within the current scope.


A variable, previously declared in the same scope.

Usage Notes

The %TYPE attribute is particularly useful when declaring variables, fields, and parameters that refer to database columns. Your code can keep working even when the lengths or types of the columns change.


For examples, see the following:

Example 1-15, "Using a PL/SQL Collection Type"
Example 2-6, "Using %TYPE With the Datatype of a Variable"
Example 2-7, "Using %TYPE With Table Columns"
Example 2-15, "Using a Subprogram Name for Name Resolution"
Example 2-10, "Assigning Values to a Record With a %ROWTYPE Declaration"
Example 3-11, "Using SUBTYPE With %TYPE and %ROWTYPE"
Example 5-5, "Declaring a Procedure Parameter as a Nested Table"
Example 5-7, "Specifying Collection Element Types with %TYPE and %ROWTYPE"
Example 5-42, "Declaring and Initializing Record Types"
Example 6-1, "Data Manipulation With PL/SQL"
Example 6-13, "Fetching Bulk Data With a Cursor"
Example 13-1, "Declaring and Assigning Values to Variables"

Related Topics

"Constant and Variable Declaration"
"%ROWTYPE Attribute"