473,769 Members | 5,601 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 7828
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
3862
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 malloc to do dynamic allocation from some static pool isn't really an option, for various reasons, not...
4
2476
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
3440
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
1603
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
2757
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 useful tracebacks on the other. I want both! How do you guys do it? What's the pythonic way? Are...
8
194015
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 14b vbDecimal(14) CDec() Integer 2b vbInteger(2) ...
0
2400
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 doesn't load back to the form. The multiview is located inside the LoginView's Logged-In View ....
19
248257
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. Assumptions - Basic HTML knowledge. - Basic PHP knowledge. HTML Forms A common and simple way of...
10
2206
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
10781
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 a new concept, there is a concept called Steganography which enables to conceal your secret...
0
9587
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8870
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3958
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
2
3561
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.