Java user-defined table functions

DB2® provides the ability for a function to return a table. This is useful for exposing information from outside the database to the database in table form. For example, a table can be created that exposes the properties set in the Java™ virtual machine (JVM) used for Java stored procedures and Java UDFs (both table and scalar).

The SQLJ Part 1: SQL Routines standard does support table functions. Consequently, table functions are only available using parameter style DB2GENERAL.

Five different types of calls are made to a table function. The following table explains these calls. These assume that scratchpad has been specified on the create function SQL statement.

Point in scan time NO FINAL CALL LANGUAGE JAVA SCRATCHPAD FINAL CALL LANGUAGE JAVA SCRATCHPAD
Before the first OPEN of the table function No calls Class constructor is called (means new scratchpad). UDF method is called with FIRST call.
At each OPEN of the table function. Class constructor is called (means new scratchpad). UDF method is called with OPEN all. UDF method is called with OPEN call.
At each FETCH for a new row of table function data. UDF method is called with FETCH call. UDF method is called with FETCH call.
At each CLOSE of the table function UDF method is called with CLOSE call. The close() method, if it exists, is also called. UDF method is called with CLOSE call.
After the last CLOSE of the table function. No calls UDF method is called with FINAL call. The close() method, if it exists, is also called.

Example: Java table function

The following is an example of a Java table function that determines the properties set in the JVM used to run the Java user-defined table function.

Note: Read the Code example disclaimer for important legal information.
     import com.ibm.db2.app.*;
     import java.util.*;

     public class JVMProperties extends UDF {
      Enumeration propertyNames;
      Properties properties ;

      public void dump (String property, String value) throws Exception
      {
        int callType = getCallType(); 
        switch(callType) {
          case SQLUDF_TF_FIRST:
            break;
          case SQLUDF_TF_OPEN:
            properties = System.getProperties(); 
            propertyNames = properties.propertyNames();
            break;
          case SQLUDF_TF_FETCH:
            if (propertyNames.hasMoreElements()) {
                property = (String) propertyNames.nextElement();
                value = properties.getProperty(property); 
                set(1, property);
                set(2, value);
            } else {
               setSQLstate("02000"); 
            }
            break;
          case SQLUDF_TF_CLOSE:
            break;
          case SQLUDF_TF_FINAL:
            break;
          default:
            throw new Exception("UNEXPECT call type of "+callType);
        } 
      }
     }

After the table function is compiled, and its class file copied to /QIBM/UserData/OS400/SQLLib/Function, the function can be registered to the database by using the following SQL statement.

     create function properties()
     returns table (property varchar(500), value varchar(500))
     external name 'JVMProperties.dump' language java
     parameter style db2general fenced no sql
     disallow parallel scratchpad

After the function has been registered, it can be used as part of an SQL statement. For example, the following SELECT statement returns the table generated by the table function.

     SELECT * FROM TABLE(PROPERTIES())
Related concepts
Restrictions on Java user-defined functions