473,396 Members | 1,990 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,396 software developers and data experts.

user defined function and executing sql statements

Hi

I want to write a user defined function which does the following.

1. Take a string value as input parameter
2. Should execute a sql statement: select col1, col2, col3, col4 from
table_name where name = value
3. Do some arithmetic operations on the values returned by the above
sql statement.
4. Return the above arithmetic operation as a float value

I am currently stuck in step 2. I dont know how to obtain the values
from the sql statement executed in step 2.

Any pointers will be very helpful.

Here is the template UDF code:

CREATE FUNCTION HEALTHCAREDB.FUNCTION1( patient_id VARCHAR(36) )
RETURNS INTEGER
F1: BEGIN ATOMIC

/*how do i read the results from within the UDF for the below sql
statement*/

SELECT col1, col2, col3, col4 FROM HEALTHCAREDB.PATIENT_VISIT_INFO_VIEW
AS WHERE PATIENT_VISIT_INFO_VIEW.PATIENT_ID = FUNCTION1.patient_id;

RETURN xxxxx;
END

Really appreciate your help.

Thanks
Mahesh

Dec 1 '06 #1
4 7645
Mahesh S wrote:
Hi

I want to write a user defined function which does the following.

1. Take a string value as input parameter
2. Should execute a sql statement: select col1, col2, col3, col4 from
table_name where name = value
3. Do some arithmetic operations on the values returned by the above
sql statement.
4. Return the above arithmetic operation as a float value

I am currently stuck in step 2. I dont know how to obtain the values
from the sql statement executed in step 2.

Any pointers will be very helpful.

Here is the template UDF code:

CREATE FUNCTION HEALTHCAREDB.FUNCTION1( patient_id VARCHAR(36) )
RETURNS INTEGER
F1: BEGIN ATOMIC

/*how do i read the results from within the UDF for the below sql
statement*/

SELECT col1, col2, col3, col4 FROM HEALTHCAREDB.PATIENT_VISIT_INFO_VIEW
AS WHERE PATIENT_VISIT_INFO_VIEW.PATIENT_ID = FUNCTION1.patient_id;

RETURN xxxxx;
END

Really appreciate your help.

Thanks
Mahesh
Look at the documentation for the SELECT INTO syntax. That is how to
get values into a local variable.

SELECT col1, col2 INTO var1, var2....

B.

Dec 1 '06 #2
Mahesh S wrote:
Hi

I want to write a user defined function which does the following.

1. Take a string value as input parameter
2. Should execute a sql statement: select col1, col2, col3, col4 from
table_name where name = value
3. Do some arithmetic operations on the values returned by the above
sql statement.
4. Return the above arithmetic operation as a float value

I am currently stuck in step 2. I dont know how to obtain the values
from the sql statement executed in step 2.

Any pointers will be very helpful.

Here is the template UDF code:

CREATE FUNCTION HEALTHCAREDB.FUNCTION1( patient_id VARCHAR(36) )
RETURNS INTEGER
This is not a FLOAT or DOUBLE PRECISION...
F1: BEGIN ATOMIC

/*how do i read the results from within the UDF for the below sql
statement*/

SELECT col1, col2, col3, col4 FROM HEALTHCAREDB.PATIENT_VISIT_INFO_VIEW
AS WHERE PATIENT_VISIT_INFO_VIEW.PATIENT_ID = FUNCTION1.patient_id;

RETURN xxxxx;
END
Use the original SQL approach and apply your operations and aggregation
directly on the subselect in the routine body, e.g:

CREATE FUNCTION ...
RETURNS INTEGER
RETURN SELECT AVG(value)
FROM ( SELECT group_id, ( AVG(col1) + SUM(col2) ) / MIN(col3)
FROM ...
GROUP BY group_id ) AS t

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 1 '06 #3
Ideally try to get to what Knut proposes.
If you do need to do procedural logic you can use a FOR loop:

BEGIN ATOMIC
DECLARE x int default 0;
FOR myrow AS SELECT c1 FROM T DO
SET x = myrow.c1 + x;
END FOR;
RETURN x;
END

If you want to do some serious lifting I recommend you write a stored
procedure and call it from the UDF. This could include processing
dynamic SQL, cursors, or condition handling.

CREATE PROCEDURE p(IN arg INT, OUT res INT)
....

CREATE FUNCTION foo(arg)
BEGIN ATOMIC
DECLARE res INT;
CALL p(arg, res);
RETURN res;
END

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

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 1 '06 #4
Thanks a lot for the suggestions.. that helps..

I have now got the UDF working.

Regards

Dec 4 '06 #5

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

Similar topics

2
by: Spam sucks | last post by:
Hello, If somebody could help me with this one it would be very nice, the story: I need to check if a user is online or not at the moment i am doing a user logoff with javascript but this is...
2
by: Steve D | last post by:
I've looked all over but can't find a solid answer. I've got a function that runs from a View and when the function runs the first time it is calculating a Temperature for a group of Formulas. ...
4
by: Sameer Deshpande | last post by:
How do I create and return user defined data types in DB2. F.ex In Oracle I can create a user define datatype and return this data type from stored function. How can I do the same in DB2? ...
2
by: David Emme | last post by:
Access 97 I have a number of SELECT statements which contain references to user-defined VBA functions. These typically work as expected, but occasionally, on one user's machine or another,...
7
by: deko | last post by:
I have a function with a number of long loops. While the function is running, I want to be able to click a Stop button and exit the function as quickly as possible. The abbreviated code looks...
6
by: jeet_sen | last post by:
Hi, I have generated a table and have attached a pop up to display at onmouseover event of each cell. For each cell the pop up will display cell specific detailed data. I have generated the pop...
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
5
by: Richard Maher | last post by:
Hi, Here I mean "User" in the Programmer or Javascript sense. I merely wish to programmatically trigger an Event. It would be absolutely fantastic if there was a (Form level?) ONUSEREVENT() and...
2
by: Naya | last post by:
Hi. I am trying to write a program that asks for the wholesale cost of an item and its markup percentage, and displays the retail price. Well, I did that the regular way, but... My...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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...

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.