95 lines
6.9 KiB
HTML
95 lines
6.9 KiB
HTML
<?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="Use user-defined functions (UDFs)" />
|
|
<meta name="abstract" content="In writing SQL applications, you can implement some actions or operations as a UDF or as a subroutine in your application: Although it may appear easier to implement new operations as subroutines in your application, you might want to consider the advantages of using a UDF instead." />
|
|
<meta name="description" content="In writing SQL applications, you can implement some actions or operations as a UDF or as a subroutine in your application: Although it may appear easier to implement new operations as subroutines in your application, you might want to consider the advantages of using a UDF instead." />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyroutines.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyudfduvc.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyudfsql.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafywudfextern.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyuwexam.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyudfuudf.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyusrdeffun.htm" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="DC.Format" content="XHTML" />
|
|
<meta name="DC.Identifier" content="rbafyudf" />
|
|
<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>Use user-defined functions (UDFs)</title>
|
|
</head>
|
|
<body id="rbafyudf"><a name="rbafyudf"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Use user-defined functions (UDFs)</h1>
|
|
<div><p>In writing SQL applications, you can implement some actions or
|
|
operations as a UDF or as a subroutine in your application: Although it may
|
|
appear easier to implement new operations as subroutines in your application,
|
|
you might want to consider the advantages of using a UDF instead.</p>
|
|
<p>For example, if the new operation is something that other users or programs
|
|
can take advantage of, a UDF can help to reuse it. In addition, the function
|
|
can be called directly in SQL wherever an expression can be used. The database
|
|
takes care of many data type promotions of the function arguments automatically.
|
|
For example, with DECIMAL to DOUBLE, the database allows your function to
|
|
be applied to different, but compatible data types.</p>
|
|
<p>In certain cases, calling the UDF directly from the database engine instead
|
|
of from your application can have a considerable performance advantage. You
|
|
will notice this advantage in cases where the function may be used in the
|
|
qualification of data for further processing. These cases occur when the function
|
|
is used in row selection processing. </p>
|
|
<p>Consider a simple scenario where you want to process some data. You can
|
|
meet some selection criteria which can be expressed as a function <samp class="codeph">SELECTION_CRITERIA()</samp>.
|
|
Your application can issue the following select statement:</p>
|
|
<pre> <strong>SELECT</strong> A, B, C <strong>FROM</strong> T</pre>
|
|
<p>When it receives each row, it runs the program's <samp class="codeph">SELECTION_CRITERIA</samp> function
|
|
against the data to decide if it is interested in processing the data further.
|
|
Here, every row of table T must be passed back to the application. But,
|
|
if <samp class="codeph">SELECTION_CRITERIA()</samp> is implemented as a UDF, your application
|
|
can issue the following statement:</p>
|
|
<pre> <strong>SELECT</strong> C <strong>FROM</strong> T <strong>WHERE</strong> SELECTION_CRITERIA(A,B)=1</pre>
|
|
<p>In this case, only the rows and one column of interest are passed across
|
|
the interface between the application and the database.</p>
|
|
<p>Another case where a UDF can offer a performance benefit is when dealing
|
|
with Large Objects (LOB). Suppose you have a function that extracts some
|
|
information from a value of a LOB. You can perform this extraction right
|
|
on the database server and pass only the extracted value back to the application.
|
|
This is more efficient than passing the entire LOB value back to the application
|
|
and then performing the extraction. The performance value of packaging this
|
|
function as a UDF can be enormous, depending on the particular situation.
|
|
</p>
|
|
</div>
|
|
<div>
|
|
<ul class="ullinks">
|
|
<li class="ulchildlink"><strong><a href="rbafyudfduvc.htm">UDF concepts</a></strong><br />
|
|
This topic is a discussion of the important concepts you need to know before coding UDFs.</li>
|
|
<li class="ulchildlink"><strong><a href="rbafyudfsql.htm">Write UDFs as SQL functions</a></strong><br />
|
|
SQL functions are UDFs that you have defined, written, and registered using the CREATE FUNCTION SQL statement.</li>
|
|
<li class="ulchildlink"><strong><a href="rbafywudfextern.htm">Write UDFs as external functions</a></strong><br />
|
|
You can write the executable code of a UDF in a language other than SQL.</li>
|
|
<li class="ulchildlink"><strong><a href="rbafyuwexam.htm">Examples: UDF code</a></strong><br />
|
|
These examples show how to implement UDF code by using SQL functions and external functions.</li>
|
|
<li class="ulchildlink"><strong><a href="rbafyudfuudf.htm">Use UDFs in SQL statements</a></strong><br />
|
|
Scalar and column UDFs can be called within an SQL statement almost everywhere that an expression is valid. Table UDFs can be called in the FROM clause of a SELECT. There are a few restrictions of UDF usage, however.</li>
|
|
</ul>
|
|
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafyroutines.htm" title="Routines are pieces of code or programs that you can call to perform operations.">Routines</a></div>
|
|
</div>
|
|
<div class="relconcepts"><strong>Related concepts</strong><br />
|
|
<div><a href="rbafyusrdeffun.htm" title="A user-defined function is a program that can be called like any built-in function.">User-defined functions</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |