108 lines
6.4 KiB
HTML
108 lines
6.4 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: Cast between UDTs" />
|
|
<meta name="abstract" content="Suppose you want to define a UDF that converts Canadian dollars to U.S. dollars." />
|
|
<meta name="description" content="Suppose you want to define a UDF that converts Canadian dollars to U.S. dollars." />
|
|
<meta name="DC.subject" content="UDTs (User-defined types), casting between UDTs, examples, sourced UDF, sourced function, UDFs (User-defined functions), sourced" />
|
|
<meta name="keywords" content="UDTs (User-defined types), casting between UDTs, examples, sourced UDF, sourced function, UDFs (User-defined functions), sourced" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyexampudt.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="rbafycastudt" />
|
|
<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: Cast between UDTs</title>
|
|
</head>
|
|
<body id="rbafycastudt"><a name="rbafycastudt"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Cast between UDTs</h1>
|
|
<div><p>Suppose you want to define a UDF that converts Canadian dollars
|
|
to U.S. dollars.</p>
|
|
<div class="section"><p>Suppose you can obtain the current exchange rate from a file managed
|
|
outside of DB2<sup>®</sup>.
|
|
Then define a UDF that obtains a value in Canadian dollars, accesses the
|
|
exchange rate file and returns the corresponding amount in U.S. dollars. </p>
|
|
</div>
|
|
<div class="section"><p>At first glance, such a UDF may appear easy to write. However,
|
|
not all C compilers support DECIMAL values. The UDTs representing different
|
|
currencies have been defined as DECIMAL. Your UDF will need to receive and
|
|
return DOUBLE values, since this is the only data type provided by C that
|
|
allows the representation of a DECIMAL value without losing the decimal precision.
|
|
Your UDF should be defined as follows: </p>
|
|
<pre> <strong>CREATE FUNCTION</strong> CDN_TO_US_DOUBLE(<strong>DOUBLE</strong>) <strong>RETURNS DOUBLE</strong>
|
|
<strong>EXTERNAL NAME</strong> 'MYLIB/CURRENCIES(C_CDN_US)'
|
|
<strong>LANGUAGE C
|
|
PARAMETER STYLE DB2SQL
|
|
NO SQL
|
|
NOT DETERMINISTIC</strong></pre>
|
|
</div>
|
|
<div class="section"><p>The exchange rate between Canadian and U.S. dollars may change
|
|
between two invocations of the UDF, so you declare it as NOT DETERMINISTIC.</p>
|
|
</div>
|
|
<div class="section"><p>The question now is, how do you pass Canadian dollars to this
|
|
UDF and get U.S. dollars from it? The Canadian dollars must be cast to DECIMAL
|
|
values. The DECIMAL values must be cast to DOUBLE. You also need to have
|
|
the returned DOUBLE value cast to DECIMAL and the DECIMAL value cast to U.S.
|
|
dollars. </p>
|
|
</div>
|
|
<div class="section"><p>Such casts are performed automatically by DB2 anytime you define sourced UDFs, whose
|
|
parameter and return type do not exactly match the parameter and return type
|
|
of the source function. Therefore, you need to define two sourced UDFs.
|
|
The first brings the DOUBLE values to a DECIMAL representation. The second
|
|
brings the DECIMAL values to the UDT. Define the following: </p>
|
|
<pre> <strong>CREATE FUNCTION</strong> CDN_TO_US_DEC (<strong>DECIMAL</strong>(9,2)) <strong>RETURNS DECIMAL</strong>(9,2)
|
|
<strong>SOURCE</strong> CDN_TO_US_DOUBLE (<strong>DOUBLE</strong>)
|
|
|
|
<strong>CREATE FUNCTION</strong> US_DOLLAR (<strong>CANADIAN_DOLLAR</strong>) <strong>RETURNS</strong> US_DOLLAR
|
|
<strong>SOURCE</strong> CDN_TO_US_DEC (<strong>DECIMAL</strong>())</pre>
|
|
</div>
|
|
<div class="section"><p>Note that an invocation of the <samp class="codeph">US_DOLLAR</samp> function
|
|
as in <samp class="codeph">US_DOLLAR(C1)</samp>, where C1 is a column whose type is Canadian
|
|
dollars, has the same effect as invoking: </p>
|
|
<pre> US_DOLLAR (<strong>DECIMAL</strong>(CDN_TO_US_DOUBLE (<strong>DOUBLE</strong> (<strong>DECIMAL</strong> (C1)))))</pre>
|
|
</div>
|
|
<div class="section"><p>That is, C1 (in Canadian dollars) is cast to decimal which in
|
|
turn is cast to a double value that is passed to the <samp class="codeph">CDN_TO_US_DOUBLE</samp> function.
|
|
This function accesses the exchange rate file and returns a double value
|
|
(representing the amount in U.S. dollars) that is cast to decimal, and then
|
|
to U.S. dollars. </p>
|
|
</div>
|
|
<div class="section"><p>A function to convert Euros to U.S. dollars is
|
|
similar to the example above: </p>
|
|
<pre> <strong>CREATE FUNCTION</strong> EURO_TO_US_DOUBLE(<strong>DOUBLE</strong>)
|
|
<strong>RETURNS DOUBLE</strong>
|
|
<strong>EXTERNAL NAME</strong> 'MYLIB/CURRENCIES(C_EURO_US)'
|
|
<strong>LANGUAGE C
|
|
PARAMETER STYLE DB2SQL
|
|
NO SQL
|
|
NOT DETERMINISTIC</strong>
|
|
|
|
<strong>CREATE FUNCTION</strong> EURO_TO_US_DEC (<strong>DECIMAL</strong>(9,2))
|
|
<strong>RETURNS DECIMAL</strong>(9,2)
|
|
<strong>SOURCE</strong> EURO_TO_US_DOUBLE(<strong>DOUBLE</strong>)
|
|
|
|
<strong>CREATE FUNCTION</strong> US_DOLLAR(EURO) <strong>RETURNS</strong> US_DOLLAR
|
|
<strong>SOURCE</strong> EURO_TO_US_DEC (<strong>DECIMAL</strong>())</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafyexampudt.htm" title="These are examples of using UDTs.">Examples: Use UDTs</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |