Example: Casting between UDTs

Suppose that you want to define a user-defined function (UDF) that converts Canadian dollars to U.S. dollars.

You can define a UDF that obtains a value in Canadian dollars, accesses the exchange rate, and returns the corresponding amount in U.S. dollars.

The UDF can be defined as follows:

     CREATE FUNCTION US_DOLLAR(CANADIAN_DOLLAR) RETURNS US_DOLLAR 
       EXTERNAL NAME 'MYLIB/CURRENCIES(C_CDN_US)' 
       LANGUAGE C 
       PARAMETER STYLE DB2SQL 
       NO SQL 
       STATEMENT DETERMINISTIC;

The exchange rate between Canadian and U.S. dollars may change between two invocations of the UDF, so you declare it as STATEMENT DETERMINISTIC.

A function to convert Euros to U.S. dollars is similar to the previous example:

     CREATE FUNCTION US_DOLLAR(EURO) RETURNS US_DOLLAR 
       EXTERNAL NAME 'MYLIB/CURRENCIES(C_EURO_US)'
       LANGUAGE C 
       PARAMETER STYLE DB2SQL 
       NO SQL 
       STATEMENT DETERMINISTIC;