95 lines
4.9 KiB
HTML
95 lines
4.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="reference" />
|
|
<meta name="DC.Title" content="Example: Counter" />
|
|
<meta name="abstract" content="Suppose you want to number the rows in your SELECT statement. So you write a UDF which increments and returns a counter." />
|
|
<meta name="description" content="Suppose you want to number the rows in your SELECT statement. So you write a UDF which increments and returns a counter." />
|
|
<meta name="DC.subject" content="examples, counter for UDFs, CREATE FUNCTION statement, counter for UDF, ctr() UDF C program listing" />
|
|
<meta name="keywords" content="examples, counter for UDFs, CREATE FUNCTION statement, counter for UDF, ctr() UDF C program listing" />
|
|
<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="rbafyexamudfcounter" />
|
|
<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: Counter</title>
|
|
</head>
|
|
<body id="rbafyexamudfcounter"><a name="rbafyexamudfcounter"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Counter</h1>
|
|
<div><p>Suppose you want to number the rows in your SELECT statement.
|
|
So you write a UDF which increments and returns a counter.</p>
|
|
<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>This example uses an external function with DB2<sup>®</sup> SQL parameter
|
|
style and a scratchpad.</p>
|
|
</div>
|
|
<div class="example"> <pre><strong>CREATE FUNCTION</strong> COUNTER()
|
|
<strong>RETURNS INT
|
|
SCRATCHPAD
|
|
NOT DETERMINISTIC
|
|
NO SQL
|
|
NO EXTERNAL ACTION
|
|
LANGUAGE C
|
|
PARAMETER STYLE DB2SQL</strong>
|
|
<strong>EXTERNAL NAME</strong> 'MYLIB/MATH(ctr)'
|
|
<strong>DISALLOW PARALLEL</strong>
|
|
|
|
/* structure scr defines the passed scratchpad for the function "ctr" */
|
|
struct scr {
|
|
long len;
|
|
long countr;
|
|
char not_used[96];
|
|
};
|
|
|
|
void ctr (
|
|
long *out, /* output answer (counter) */
|
|
short *outnull, /* output NULL indicator */
|
|
char *sqlstate, /* SQL STATE */
|
|
char *funcname, /* function name */
|
|
char *specname, /* specific function name */
|
|
char *mesgtext, /* message text insert */
|
|
struct scr *scratchptr) { /* scratch pad */
|
|
|
|
*out = ++scratchptr->countr; /* increment counter & copy out */
|
|
*outnull = 0;
|
|
return;
|
|
}
|
|
/* end of UDF : ctr */</pre>
|
|
</div>
|
|
<div class="section"><p>For this UDF, observe that:</p>
|
|
<ul><li>It has no input SQL arguments defined, but returns a value.</li>
|
|
<li>It appends the scratchpad input argument after the four standard trailing
|
|
arguments, namely <em>SQL-state</em>, <em>function-name</em>, <em>specific-name</em>,
|
|
and <em>message-text</em>. </li>
|
|
<li>It includes a structure definition to map the scratchpad which is passed.</li>
|
|
<li>No input parameters are defined. This agrees with the code.</li>
|
|
<li>SCRATCHPAD is coded, causing DB2 to allocate, properly initialize and
|
|
pass the scratchpad argument.</li>
|
|
<li>You have specified it to be NOT DETERMINISTIC, because it depends on more
|
|
than the SQL input arguments, (none in this case).</li>
|
|
<li><span>You have correctly specified DISALLOW PARALLEL, because correct functioning
|
|
of the UDF depends on a single scratchpad.</span></li>
|
|
</ul>
|
|
</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> |