473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

UDF with exception handler possible ?

N
Hi all,
I am wondering if there is a way to
create a UDF with exception handler.
I've been trying to find an example in the
doc and online without any lucks. What
I found is that I will need to create an
SPL with an OUT parameter and CALL
it. But what I need is something that acts
like a function so that I can use it in the
SELECT statement etc. Here is an example.
------------------
CREATE PROCEDURE db2.get_type_id (v_type CHAR(2),
OUT RETURN_VAL INTEGER)
LANGUAGE SQL
BEGIN
DECLARE v_cat_type INTEGER;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION,

SQLWARNING,
NOT
FOUND
BEGIN
SET v_cat_type = 7;
END;
IF v_type <> '' THEN
SET v_cat_type = DOUBLE(v_type);
ELSE
SET v_cat_type = 7;
END IF;
SET RETURN_VAL = v_cat_type;
RETURN 0;
END
--------------------
It would be great if I could create the above SPL as a
FUNCTION instead (with HANDLER). What I would like to
be able to do is being able to :

SELECT
col_id,
db2.get_type_id (col_type_id)
FROM
db2.table_name;

Thanks for any helps/inputs anyone can give!!!

N.
Nov 12 '05 #1
3 3275
N wrote:

db2 -td%
CREATE PROCEDURE db2.get_type_id (v_type CHAR(2),
OUT RETURN_VAL INTEGER)
LANGUAGE SQL CONTAINS SQL BEGIN
DECLARE v_cat_type INTEGER;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION,

SQLWARNING,
NOT FOUND
BEGIN
SET v_cat_type = 7;
END;
IF v_type <> '' THEN
SET v_cat_type = DOUBLE(v_type);
ELSE
SET v_cat_type = 7;
END IF;
SET RETURN_VAL = v_cat_type;
RETURN 0;
END %

CREATE FUNCTION db2.get_type_id (v_type CHAR(2)
RETURNS INTEGER
CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION
BEGIN ATOMIC
DECLARE RETURN_VAL INTEGER;
CALL db2.get_type_id (v_type, RETURN_VAL);
RETURN RETURN_VAL;
END
% SELECT
col_id,
db2.get_type_id (col_type_id)
FROM
db2.table_name

%

Cheers
Serge
Nov 12 '05 #2
N
Thank you Serge,
Is this the only way out ? So I have to have an SPL
for every functions I have ? We have well over 300
functions....

Thank you!

"Serge Rielau" <sr*****@ca.ibm.com> wrote in message
news:2t*************@uni-berlin.de...
N wrote:

db2 -td%
CREATE PROCEDURE db2.get_type_id (v_type CHAR(2),
OUT RETURN_VAL INTEGER)
LANGUAGE SQL

CONTAINS SQL
BEGIN
DECLARE v_cat_type INTEGER;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION,

SQLWARNING,
NOT FOUND
BEGIN
SET v_cat_type = 7;
END;
IF v_type <> '' THEN
SET v_cat_type = DOUBLE(v_type);
ELSE
SET v_cat_type = 7;
END IF;
SET RETURN_VAL = v_cat_type;
RETURN 0;
END

%

CREATE FUNCTION db2.get_type_id (v_type CHAR(2)
RETURNS INTEGER
CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION
BEGIN ATOMIC
DECLARE RETURN_VAL INTEGER;
CALL db2.get_type_id (v_type, RETURN_VAL);
RETURN RETURN_VAL;
END
%
SELECT
col_id,
db2.get_type_id (col_type_id)
FROM
db2.table_name

%

Cheers
Serge

Nov 12 '05 #3
Are all these 300 functions so complex that they need condition handlers?
SQL Functions are supposed to be simple since they are meant to execute
very often (e.g. in a WHERE clause)....

As soon as you use a feature that is not supported in inline SQL PL you
need to call out to a stored procedure.
In some future the goal is to let the user choose between inline SQL PL
and regular SQL PL in the CREATE FUNCTION statement.
But we are not quite there yet ...

Cheers
Serge
Nov 12 '05 #4

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

Similar topics

0
by: Derek Wolf | last post by:
I am having some difficulties implementing an UnHandledException handler for the AppDomain.UnhandledException event in a windows service for unhandled exceptions that occur on a System.Timers.Timer...
1
by: Nick | last post by:
I am working on a VB.NET application and instead of throwing Try Catch blocks all over the place, I have a custom applcation context which has a exception handler.. Public Class ContextManager...
14
by: Nenad Dobrilovic | last post by:
Hi, Is it possible for exception object to be aware of it's throwing? I want to log in the text file when exeption is thrown, not when the exception object is created (because I can create...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
15
by: Mark Lewis | last post by:
I have a weird error trapping problem. When running the IDE everything works fine but not when running in an EXE I get the Unhandled Exception Error message box intead of the one in my Try....Catch...
6
by: daniel | last post by:
I use a simple program to illustrate the problem: import logging def foo() : raise ValueError("foo") if __name__ == "__main__" : try : foo()
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
6
by: colin | last post by:
Is it possible to do this ? i cant see any likly options. I have an exception handler but I want to examine the exception before it gets to the handler, Is there a way to do this ? At the moment...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.