ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzaha_5.4.0.1/udftable.htm

141 lines
7.6 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="concept" />
<meta name="DC.Title" content="Java user-defined table functions" />
<meta name="abstract" content="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)." />
<meta name="description" content="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)." />
<meta name="DC.Relation" scheme="URI" content="writeudf.htm" />
<meta name="DC.Relation" scheme="URI" content="udfrestr.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="udftable" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Java user-defined table functions</title>
</head>
<body id="udftable"><a name="udftable"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Java user-defined table functions</h1>
<div><p>DB2<sup>®</sup> 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).</p>
<p>The <em>SQLJ Part 1: SQL Routines</em> standard does support table functions.
Consequently, table functions are only available using parameter style DB2GENERAL.</p>
<p>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.</p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" width="100%" frame="border" border="1" rules="all"><thead align="left"><tr><th align="left" valign="top" id="d0e39">Point in scan time</th>
<th valign="top" id="d0e41">NO FINAL CALL LANGUAGE JAVA SCRATCHPAD</th>
<th align="left" valign="top" id="d0e43">FINAL CALL LANGUAGE JAVA SCRATCHPAD</th>
</tr>
</thead>
<tbody><tr><td valign="top" headers="d0e39 ">Before the first OPEN of the table function</td>
<td valign="top" headers="d0e41 ">No calls</td>
<td valign="top" headers="d0e43 ">Class constructor is called (means new scratchpad). UDF method is called
with FIRST call. </td>
</tr>
<tr><td valign="top" headers="d0e39 ">At each OPEN of the table function.</td>
<td valign="top" headers="d0e41 ">Class constructor is called (means new scratchpad). UDF method is called
with OPEN all. </td>
<td valign="top" headers="d0e43 ">UDF method is called with OPEN call. </td>
</tr>
<tr><td valign="top" headers="d0e39 ">At each FETCH for a new row of table function data. </td>
<td valign="top" headers="d0e41 ">UDF method is called with FETCH call. </td>
<td valign="top" headers="d0e43 ">UDF method is called with FETCH call. </td>
</tr>
<tr><td valign="top" headers="d0e39 ">At each CLOSE of the table function</td>
<td valign="top" headers="d0e41 ">UDF method is called with CLOSE call. The close() method, if it exists,
is also called.</td>
<td valign="top" headers="d0e43 ">UDF method is called with CLOSE call.</td>
</tr>
<tr><td valign="top" headers="d0e39 ">After the last CLOSE of the table function. </td>
<td valign="top" headers="d0e41 ">No calls</td>
<td valign="top" headers="d0e43 ">UDF method is called with FINAL call. The close() method, if it exists,
is also called.</td>
</tr>
</tbody>
</table>
</div>
<div class="section"><h4 class="sectiontitle">Example: Java table function</h4><p>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.</p>
<div class="note"><span class="notetitle">Note:</span> Read
the <a href="codedisclaimer.htm">Code example disclaimer</a> for important
legal information.</div>
<pre> 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);
}
}
}</pre>
<p>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.</p>
<pre> 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</pre>
<p>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.</p>
<pre> SELECT * FROM TABLE(PROPERTIES())</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="writeudf.htm" title="A Java scalar function returns one value from a Java program to the database. For example, a scalar function could be created that returns the sum of two numbers.">Java user-defined scalar functions</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="udfrestr.htm" title="These restrictions apply to Java user-defined functions (UDFs).">Restrictions on Java user-defined functions</a></div>
</div>
</div>
</body>
</html>