A user-defined function

If you know the name of the user-defined function at compile time, execute the user-defined function (external or SPL) with the EXECUTE FUNCTION statement. In the INTO clause of EXECUTE FUNCTION, you list the host variables that hold the return value or values. The following EXECUTE FUNCTION statement executes a user-defined function called items_pct() (which Code for items_pct SPL function defines):
EXEC SQL execute function items_pct(\"HSK\")
   into :manuf_percent;

If you do not know the name of the user-defined function until run time, you must dynamically execute the function. Dynamic execution of a user-defined function is a similar dynamic execution of a SELECT statement (Handling an unknown select list). Both the SELECT and the user-defined function return values to the program.

Execute a user-defined function with the EXECUTE FUNCTION statement. You can execute an EXECUTE FUNCTION statement in the following two ways:
  • If the user-defined function returns only one row, use PREPARE and EXECUTE INTO to execute the EXECUTE FUNCTION statement. This type of user-defined function is often called a noncursor function.
  • If the user-defined function returns more than one row, you must declare a function cursor to execute the EXECUTE FUNCTION statement.

    This type of user-defined function is often called a cursor function. A cursor function that is written in SPL (an SPL function) has the WITH RESUME clause in its RETURN statement. A cursor function that is written in an external language such as C is an iterator function.

Tip: If you do not know the data type of the return value, you must use a dynamic-management structure to hold the value.