Application Development Guide

Description

This section describes how to write UDFs and methods. The coding conventions for UDFs and methods are the same, with the following differences:

As the guidelines for writing UDFs and methods are the same, with the exception of the previously described difference, the remainder of the discussion on writing UDFs and methods refers to both UDFs and methods simply as UDFs.

For small UDFs such as UDFs that contain only a simple expression, consider using a SQL-bodied UDF. To create a SQL-bodied UDF, issue a CREATE FUNCTION or CREATE METHOD statement that includes a method body written using SQL, rather than pointing to an external UDF. SQL-bodied UDFs enable you to declare and define the UDF in a single step, without using an external language or compiler. SQL-bodied UDFs also offer the possibility of increased performance, because the method body is written using SQL accessible to the DB2 optimizer.

The following example demonstrates a simple CREATE FUNCTION statement that creates a SQL-bodied UDF:

   CREATE FUNCTION tan(double x)
   RETURNS double
   NO EXTERNAL ACTION
   DETERMINISTIC
   LANGUAGE SQL
   CONTAINS SQL
   RETURN sin(x) / cos(x);

For further information on SQL-bodied functions, refer to the SQL Reference.

After a preliminary discussion on the interface between DB2 and a UDF, the remaining discussion concerns how you implement UDFs. The information on writing the UDF emphasizes the presence or absence of a scratchpad as one of the primary considerations.

Some general considerations in using this section are:

Note that a sourced UDF, which is different from an external UDF, does not require an implementation in the form of a separate piece of code. Such a UDF uses the same implementation as its source function, along with many of its other attributes.


[ Top of Page | Previous Page | Next Page ]