473,543 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new DB2 data type

guys help me on this rather nubish question.I have created a new data
type in java and i also want to create a new data type object in db2
that will store the money data object. The money implementation takes
2 arguments and returns. I hava read abt user defined structured data
types but i seem not to understand their work. anyone pliz help. my db
is \

DB2 administration tools level:
Product identifier SQL08024
Level identifier 03050106
Level DB2 v8.1.2.104
Build level s060120
PTF MI00152
=============== =============== =============== ===============
Java development kit (JDK):
Level IBM Corporation 1.4.2
=============== =============== =============== ===============

Sep 1 '06 #1
5 7819
Do you need to store the currency with the value in the table? It seems
very awkward not to normalize into one currency.
User defined DISTINCT types (typedef) are much easier to handle and
perform better than structured types.

CREATE DISTINCT TYPE euro AS DECIMAL(15, 2) WITH COMPARISONS;
CREATE FUNCTION euro(value DECIMAL(15, 2), currency VARCHAR(1))
RETURNS EURO
DETERMINISTIC READS SQL DATA
RETURN EURO(value * (SELECT rate FROM conversion
WHERE conversion.curr ency = euro.currency)) ;

I leave it to Knut to give a structure type example :-)
Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Sep 1 '06 #2
Serge Rielau wrote:
Do you need to store the currency with the value in the table? It seems
very awkward not to normalize into one currency.
User defined DISTINCT types (typedef) are much easier to handle and
perform better than structured types.

CREATE DISTINCT TYPE euro AS DECIMAL(15, 2) WITH COMPARISONS;
CREATE FUNCTION euro(value DECIMAL(15, 2), currency VARCHAR(1))
RETURNS EURO
DETERMINISTIC READS SQL DATA
RETURN EURO(value * (SELECT rate FROM conversion
WHERE conversion.curr ency = euro.currency)) ;

I leave it to Knut to give a structure type example :-)
CREATE TYPE money_value
AS (
value DECIMAL(20, 2),
currency VARCHAR(3)
)
INSTANTIABLE
MODE DB2SQL
NOT FINAL;

ALTER TYPE money_value
ADD METHOD getEuro()
RETURNS DECIMAL(20, 2)
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
READS SQL DATA
ADD METHOD getUSDollar()
RETURNS DECIMAL(20, 2)
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
READS SQL DATA;

CREATE METHOD getEuro()
FOR money_value
RETURN ( SELECT SELF.value * c.exchange_rate
FROM conversions AS c
WHERE c.source_curren cy = SELF.currency AND
c.target_curren cy = 'EUR' );

CREATE METHOD getUSDollar()
FOR money_value
RETURN ( SELECT SELF.value * c.exchange_rate
FROM conversions AS c
WHERE c.source_curren cy = SELF.currency AND
c.target_curren cy = 'USD' );

CREATE FUNCTION money_value(val DECIMAL(20, 2), cur VARCHAR(3))
RETURN money_value
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
RETURN CASE
WHEN NOT EXISTS ( SELECT 1
FROM conversions
WHERE source_currency = cur )
THEN ( SELECT RAISE_ERROR('MO CUR',
'Invalid currency ''' || cur || '''')
FROM sysibm.sysdummy 1
WHERE NOT EXISTS ( SELECT 1
FROM conversions
WHERE source_currency = cur ) )
ELSE money_value().. value(val)..cur rency(cur)
END;

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Sep 1 '06 #3
sorrry Serge for being a bother, am noob in db2. thanks for the
invaluable solution. how to i go about it, ie where do i go and put the
code specified below. also what tables do i have to create first, and
with what columns.
Serge Rielau wrote:
Do you need to store the currency with the value in the table? It seems
very awkward not to normalize into one currency.
User defined DISTINCT types (typedef) are much easier to handle and
perform better than structured types.

CREATE DISTINCT TYPE euro AS DECIMAL(15, 2) WITH COMPARISONS;
CREATE FUNCTION euro(value DECIMAL(15, 2), currency VARCHAR(1))
RETURNS EURO
DETERMINISTIC READS SQL DATA
RETURN EURO(value * (SELECT rate FROM conversion
WHERE conversion.curr ency = euro.currency)) ;

I leave it to Knut to give a structure type example :-)
Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Sep 2 '06 #4
Akaketwa wrote:
sorrry Serge for being a bother, am noob in db2. thanks for the
invaluable solution. how to i go about it, ie where do i go and put the
code specified below. also what tables do i have to create first, and
with what columns.
I merely gave an example. What are your plans today for the currency?
Are you perhaps planning to use a web service to get up to date currency
conversion rates?

The statements I posted could be run from the command editor or the CLP
if you don't like GUIs.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Sep 2 '06 #5
Akaketwa wrote:
sorrry Serge for being a bother, am noob in db2. thanks for the
invaluable solution. how to i go about it, ie where do i go and put the
code specified below. also what tables do i have to create first, and
with what columns.
Your response suggests to me that you should first start with the basics of
DB2 and SQL before you work with more advanced features like user-defined
data types and routines.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Sep 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3841
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system that doesn't offer dynamic memory allocation (to be clear: no malloc, no realloc), and with rather tight memory constraints. Writing my own...
4
2466
by: Dan | last post by:
I'm trying to creat a data structure, that can be either a integer, double, string, or linked list. So I created the following, but don't know if it is the data structure itself causing problems, or something I am doing in the rest of the program. This is the data structure. struct node { char type;
11
3414
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the difference between data type 'CHAR' and 'TEXT'? When do you use 'VAR' in your datatype word? e.g. VARCHAR ?
4
1587
by: toby989 | last post by:
Hi All Anyone came across this problem before? I have an X<L file created by excel <xml id="model"> <root> <Row>
18
2727
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning from the experts. I feel there's a tradeoff between clear, easily readdable and extensible code on one side, and safe code providing early errors and...
8
193956
MMcCarthy
by: MMcCarthy | last post by:
Type MemSize RetVal of VarType() Declaration Char Conversion Boolean 2b vbBoolean(11) CBool() Byte 1b vbByte(17) CByte() Currency 8b vbCurrency(6) @ CCur() Date 8b vbDate(7) CDate() Decimal...
0
2390
by: Jacob Donajkowski | last post by:
Once the user logs in I want to have the users switch from the Roster View to the Profile View and enter their profile infomation and save it. Then the next time they login and go to the Profile View I want the form populated from there profile on the sql server. The code to save the profile works fine. But when the user logs back in they data...
19
248034
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect this data is essential to any developer. This article is a basic tutorial on how to user HTML Forms, the most common method of data collection. ...
10
2184
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which has string value. I really do not understand why push_back() function is trying to remove previously inserted data. Thanks for any help
0
10720
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not...
0
7411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7693
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5888
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5282
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3394
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1824
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.