Defining behavior for structured types
To define behaviors for structured types, you can create user-defined methods. You cannot create methods for distinct types. Creating a method is similar to creating a function, with the exception that methods are created specifically for a type, so that the type and its behavior are tightly integrated.
About this task
The method specification must be associated with the type
before you issue the CREATE METHOD statement. The following statement
adds the method specification for a method called calc_bonus
to the Employee_t
type:
ALTER TYPE Employee_t
ADD METHOD calc_bonus (rate DOUBLE)
RETURNS DECIMAL(7,2)
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC;
Once you have associated the
method specification with the type, you can define the behavior for
the type by creating the method as either an external method or an
SQL-bodied method, according to the method specification. For example,
the following statement registers an SQL method called calc_bonus
that resides in the same schema as the type Employee_t
:
CREATE METHOD calc_bonus (rate DOUBLE)
RETURNS DECIMAL(7,2)
FOR Employee_t
RETURN SELF..salary * rate;
You can create as
many methods named calc_bonus
as you like, as long
as they have different numbers or types of parameters, or are defined
for types in different type hierarchies. In other words, you cannot
create another method named calc_bonus
for Architect_t
that has the same parameter types and same number
of parameters.