Writing routines

The three types of routines (procedures, UDFs, and methods) have much in common with regards to how they are written.

Before you begin

The three routine types employ some of the same parameter styles, support the use of SQL through various client interfaces (embedded SQL, CLI, and JDBC), and can all invoke other routines. To this end, the following steps represent a single approach for writing routines.

There are some routine features that are specific to a routine type. For example, result sets are specific to stored procedures, and scratchpads are specific to UDFs and methods. When you come across a step not applicable to the type of routine you are developing, go to the step that follows it.

Before writing a routine, you must decide the following:
  • The type of routine you need.
  • The programming language you will use to write it.
  • Which interface to use if you require SQL statements in your routine.
See also the topics on Security, Library and Class Management, and Performance considerations.


To create a routine body, you must:

  1. Applicable only to external routines. Accept input parameters from the invoking application or routine and declare output parameters. How a routine accepts parameters is dependent on the parameter style you will create the routine with. Each parameter style defines the set of parameters that are passed to the routine body and the order that the parameters are passed.

    For example, the following is a signature of a UDF body written in C (using sqludf.h) for PARAMETER STYLE SQL:

                                    SQLUDF_DOUBLE *in2,
                                    SQLUDF_DOUBLE *outProduct,
                                    SQLUDF_NULLIND *in1NullInd,
                                    SQLUDF_NULLIND *in2NullInd,
                                    SQLUDF_NULLIND *productNullInd,
                                    SQLUDF_TRAIL_ARGS )
  2. Add the logic that the routine is to perform. Some features that you can employ in the body of your routines are as follows:
    • Calling other routines (nesting), or calling the current routine (recursion).
    • In routines that are defined to have SQL (CONTAINS SQL, READS SQL, or MODIFIES SQL), the routine can issue SQL statements. The types of statements that can be invoked is controlled by how routines are registered.
    • In external UDFs and methods, use scratchpads to save state from one call to the next.
    • In SQL procedures, use condition handlers to determine the SQL procedure's behavior when a specified condition occurs. You can define conditions based on SQLSTATEs.
  3. Applicable only to stored procedures. Return one or more result sets. In addition to individual parameters that are exchanged with the calling application, stored procedures have the capability to return multiple result sets. Only SQL routines and CLI, ODBC, JDBC, and SQLJ routines and clients can accept result sets.


In addition to writing your routine, you also need to register it before you can invoke it. This is done with the CREATE statement that matches the type of routine you are developing. In general, the order in which you write and register your routine does not matter. However, the registration of a routine must precede its being built if it issues SQL that references itself. In this case, for a bind to be successful, the routine's registration must have already occurred.