An anchored type defines a data type based on another SQL
object such as a column, global variable, SQL variable, SQL parameter,
or the row of a table or view.
A data type defined using an anchored type definition maintains
a dependency on the object to which it is anchored. Any change in
the data type of the anchor object will impact the anchored data type.
If anchored to the row of a table or view, the anchored data type
is ROW with the fields defined by the columns of the anchor table
or anchor view.
This data type is useful when declaring variables in cases where
you require that the variable have the same data type as another object,
for example a column in a table, but you do not know exactly what
is the data type.
An anchored data type can be of the same type as one of:
- a row in a table
- a row in a view
- a cursor variable row definition
- a column in a table
- a column in a view
- a local variable, including a local cursor variable or row variable
- a global variable
Anchored data types can only be specified when declaring or creating
one of the following:
- a local variable in an SQL procedure, including a row variable
- a local variable in a compiled SQL function, including a row variable
- a routine parameter
- a user-defined cursor data type using the CREATE TYPE statement.
- It cannot be referenced in a DECLARE CURSOR statement.
- a function return data type
- a global variable
To define an anchored data type specify the ANCHOR DATA TYPE TO
clause (or the shorter form ANCHOR clause) to specify what the data
type will be. If the anchored data type is a row data type, the ANCHOR
ROW OF clause, or one of its synonyms, must be specified. These clauses
are supported within the following statements:
- DECLARE
- CREATE TYPE
- CREATE VARIABLE
- In this version, global variables can only be anchored to other
global variables, a column in a table, or a column in a view.