By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,235 Members | 1,464 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,235 IT Pros & Developers. It's quick & easy.

new DB2 data type

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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.currency = 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

P: n/a
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.currency = 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_currency = SELF.currency AND
c.target_currency = 'EUR' );

CREATE METHOD getUSDollar()
FOR money_value
RETURN ( SELECT SELF.value * c.exchange_rate
FROM conversions AS c
WHERE c.source_currency = SELF.currency AND
c.target_currency = '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('MOCUR',
'Invalid currency ''' || cur || '''')
FROM sysibm.sysdummy1
WHERE NOT EXISTS ( SELECT 1
FROM conversions
WHERE source_currency = cur ) )
ELSE money_value()..value(val)..currency(cur)
END;

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Sep 1 '06 #3

P: n/a
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.currency = 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.