118 lines
5.0 KiB
HTML
118 lines
5.0 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="reference" />
|
|
<meta name="DC.Title" content="Example: Square of a number UDF" />
|
|
<meta name="abstract" content="In this example, suppose that you want a function that returns the square of a number." />
|
|
<meta name="description" content="In this example, suppose that you want a function that returns the square of a number." />
|
|
<meta name="DC.subject" content="examples, square of a number UDF, CREATE FUNCTION statement" />
|
|
<meta name="keywords" content="examples, square of a number UDF, CREATE FUNCTION statement" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyuwexam.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="rbafyudfexsqrnum" />
|
|
<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>Example: Square of a number UDF</title>
|
|
</head>
|
|
<body id="rbafyudfexsqrnum"><a name="rbafyudfexsqrnum"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Square of a number UDF</h1>
|
|
<div><p>In this example, suppose that you want a function that returns
|
|
the square of a number.</p>
|
|
<div class="section"><p>The query statement is:</p>
|
|
<pre><strong>SELECT</strong> SQUARE(myint) <strong>FROM</strong> mytable</pre>
|
|
</div>
|
|
<div class="section"> <div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
|
</div>
|
|
<div class="section"><p>The following examples show how to define the UDF several different
|
|
ways.</p>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Use an SQL function</h4><p>The CREATE FUNCTION statement:</p>
|
|
<pre><strong>CREATE FUNCTION</strong> SQUARE( inval <strong>INT</strong>) <strong>RETURNS INT</strong>
|
|
<strong>LANGUAGE SQL
|
|
SET OPTION DBGVIEW=*SOURCE
|
|
BEGIN
|
|
RETURN</strong>(inval*inval);
|
|
<strong>END</strong></pre>
|
|
<p>This creates an SQL function that you can debug.</p>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Use an external function, parameter style SQL</h4><p>The
|
|
CREATE FUNCTION statement: </p>
|
|
<pre><strong>CREATE FUNCTION</strong> SQUARE(<strong>INT</strong>) <strong>RETURNS INT CAST FROM FLOAT</strong>
|
|
<strong>LANGUAGE C</strong>
|
|
<strong>EXTERNAL NAME</strong> 'MYLIB/MATH(SQUARE)'
|
|
<strong>DETERMINISTIC
|
|
NO SQL
|
|
NO EXTERNAL ACTION
|
|
PARAMETER STYLE SQL
|
|
ALLOW PARALLEL</strong> </pre>
|
|
<p>The code: </p>
|
|
<pre>void SQUARE(int *inval,
|
|
double *outval,
|
|
short *inind,
|
|
short *outind,
|
|
char *sqlstate,
|
|
char *funcname,
|
|
char *specname,
|
|
char *msgtext)
|
|
{
|
|
if (*inind<0)
|
|
*outind=-1;
|
|
else
|
|
{
|
|
*outval=*inval;
|
|
*outval=(*outval)*(*outval);
|
|
*outind=0;
|
|
}
|
|
return;
|
|
}</pre>
|
|
<p>To create the external service program so it can be debugged: </p>
|
|
<pre>CRTCMOD MODULE(mylib/square) DBGVIEW(*SOURCE)
|
|
CRTSRVPGM SRVPGM(mylib/math) MODULE(mylib/square)
|
|
EXPORT(*ALL) ACTGRP(*CALLER)</pre>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Use an external function, parameter style GENERAL</h4><p>The
|
|
CREATE FUNCTION statement:</p>
|
|
<pre><strong>CREATE FUNCTION</strong> SQUARE(<strong>INT</strong>) <strong>RETURNS INT CAST FROM FLOAT</strong>
|
|
<strong>LANGUAGE C
|
|
EXTERNAL NAME</strong> 'MYLIB/MATH(SQUARE)'
|
|
<strong>DETERMINISTIC
|
|
NO SQL
|
|
NO EXTERNAL ACTION
|
|
PARAMETER STYLE GENERAL
|
|
ALLOW PARALLEL</strong> </pre>
|
|
<p>The code:</p>
|
|
<pre>double SQUARE(int *inval)
|
|
{
|
|
double outval;
|
|
outval=*inval;
|
|
outval=outval*outval;
|
|
return(outval);
|
|
}</pre>
|
|
<p>To create the external service program so it can be debugged: </p>
|
|
<pre>CRTCMOD MODULE(mylib/square) DBGVIEW(*SOURCE)
|
|
|
|
CRTSRVPGM SRVPGM(mylib/math) MODULE(mylib/square)
|
|
EXPORT(*ALL) ACTGRP(*CALLER)</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafyuwexam.htm" title="These examples show how to implement UDF code by using SQL functions and external functions.">Examples: UDF code</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |