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

Creating Scalar UDF


Hi,
I am trying to create a function which returns a scalar data. My
function tries to insert a row in a table as well.

/**

CREATE function getManufacturerId
(
manufacturer VARCHAR(128)
)
RETURNS BIGINT
Language sql
modifies sql data
BEGIN ATOMIC

declare manufacturer_row bigint;
set manufacturer_row = (select manufacturer_id from manufacturer where
manufacturer_name = manufacturer);
IF(manufacturer_row IS NULL ) THEN

set manufacturer_row = nextval for manufacturer_seq ;
INSERT INTO manufacturer VALUES(manufacturer_row, manufacturer);

END IF;
return manufacturer_row;
END@
*/

I am getting following error.

DB21034E The command was processed as an SQL statement because it was
not a valid Command Line Processor command. During SQL processing it
returned:
SQL0628N Multiple or conflicting keywords involving the "MODIFIES SQL
DATA" clause are present. LINE NUMBER=18. SQLSTATE=42613

After googling for a while i found out that only table functions can
declare "modifies sql data". This raises the question whether i can
create a table function to get only one value from the function and use
the return value in "call procedure statement".
In my procedure i have to use the above function

call getManufacturerId(manufacturer)

And the above statement will return me the result set if i use table
function, how can i use that result set in procedure to get value.

Similarly instead of using function which has plenty of restrictions,
if i create procedure can i get get one value by calling a procedure
from a procedure.

I hope I am able to explain my problem, if not please inform me i will
try to explain again.

Any hint to solve above problem will be highly appreciated.

Thanks!
--
deepdata
------------------------------------------------------------------------
deepdata's Profile: http://www.dbtalk.net/m335
View this thread: http://www.dbtalk.net/t305851

May 11 '06 #1
5 5303
deepdata wrote:
Hi,
I am trying to create a function which returns a scalar data. My
function tries to insert a row in a table as well.

/**

CREATE function getManufacturerId
(
manufacturer VARCHAR(128)
)
RETURNS BIGINT
Language sql
modifies sql data
BEGIN ATOMIC

declare manufacturer_row bigint;
set manufacturer_row = (select manufacturer_id from manufacturer where
manufacturer_name = manufacturer);
IF(manufacturer_row IS NULL ) THEN

set manufacturer_row = nextval for manufacturer_seq ;
INSERT INTO manufacturer VALUES(manufacturer_row, manufacturer);

END IF;
return manufacturer_row;
END@
*/

I am getting following error.

DB21034E The command was processed as an SQL statement because it was
not a valid Command Line Processor command. During SQL processing it
returned:
SQL0628N Multiple or conflicting keywords involving the "MODIFIES SQL
DATA" clause are present. LINE NUMBER=18. SQLSTATE=42613

After googling for a while i found out that only table functions can
declare "modifies sql data". This raises the question whether i can
create a table function to get only one value from the function and use
the return value in "call procedure statement".
In my procedure i have to use the above function

call getManufacturerId(manufacturer)

And the above statement will return me the result set if i use table
function, how can i use that result set in procedure to get value.

Similarly instead of using function which has plenty of restrictions,
if i create procedure can i get get one value by calling a procedure
from a procedure.

To return one value from a table function simply use e.g.:
....RETURNS TABLE(INT)
..... RETURN VALUES 1

But it really looks like you want a stored procedure.
Stored procedures support INOUT and OUT parameters.
E.g.
CREATE PROCEDURE plus(IN a INT, IN b INT, OUT c INT)
SET c = a + b;

When using an OUT (or INOUT) parameter you need to pass a ? or variable
so the result can be stored.

CALL plus(a, b, ?)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 11 '06 #2

Hi,

Thanks for replying.

I was thinking function would be appropriate if i have to return a
single scalar value. But with the restrictions i guess, procedure would
be best option.

The other solution which you have provided i.e.

...Returns table(int)
...Return values 1

How can I use the values return by this function table in some
procedure? Does DB2 provides way to handle resultset (i presume the
table returned from the funciton is resultset)?

If you can provide this information it will be really helpful to solve
other problem.

Thanks!
Regards,

Dipesh
--
deepdata
------------------------------------------------------------------------
deepdata's Profile: http://www.dbtalk.net/m335
View this thread: http://www.dbtalk.net/t305851

May 11 '06 #3
deepdata wrote:
I was thinking function would be appropriate if i have to return a
single scalar value. But with the restrictions i guess, procedure would
be best option. You have to start with the usage.
Procedures encapsulate application logic. It is simply an extension of
your app logic.
Functions extend SQL capabilities.
That is a procedure gets CALL-ed using the CALL statement.
A function can be invoked by any SQL statement where ever an expression
(scalar) or a table (table function) is allowed.
The other solution which you have provided i.e.

..Returns table(int)
..Return values 1

How can I use the values return by this function table in some
procedure? Does DB2 provides way to handle resultset (i presume the
table returned from the funciton is resultset)?

A table function in invoked where a table is invoked.
It is described under "table-reference" in the docs.
<table-reference>
::= <table> ....
| TABLE(<functionname>(<parameters>)) AS <tablealias>

So:
SELECT * FROM TABLE(foo(15, 'hello')) AS F
or
SELECT * FROM T LEFT OUTER JOIN TABLE(foo(T.c1, T.c2)) AS F ON T.c3 = F.A

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 11 '06 #4

Hi,

Thanks for replying. I will try that and inform you whether i am able
to do that or not.

Thanks for explaining usage of function and procedures.
If I ever need a function to return a value (single value) then i have
to create a function they way you have described and use it in the
procedure as you have described here.

Thanks!

PS: By the way which docs you are referring to. I am using db2 for the
first time. I am porting my database from firebird to db2 and reading
most of the stuff from online only.
--
deepdata
------------------------------------------------------------------------
deepdata's Profile: http://www.dbtalk.net/m335
View this thread: http://www.dbtalk.net/t305851

May 11 '06 #5
deepdata wrote:
PS: By the way which docs you are referring to. I am using db2 for the
first time. I am porting my database from firebird to db2 and reading
most of the stuff from online only.

http://publib.boulder.ibm.com/infoce...n/r0000875.htm
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 11 '06 #6

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

Similar topics

2
by: Mountain Man | last post by:
Hi, I have an array derived from a set of radio buttons that I want to break down into a single variable for use with a database. How can I do this? $gender is the array, and I want $gender2 to...
1
by: SRam | last post by:
I am coding for a server. After server is reading a particluar port, How can I create handles for them and distinguish them individually #!/usr/local/bin/perl -w use strict; use IO::Socket;...
7
by: Steve Jorgensen | last post by:
Hi all, I've been using scalar functions as a way to perform some complex data transformation operations, and I've noticed that scalar functions reaaaaalllllyyyy sloooowwwwww thiiiiiings...
7
by: roger | last post by:
I'm having difficulties invoking a user defined table function, when passing to it a parameter that is the result of another user defined function. My functions are defined like so: drop...
2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
5
by: Bob Stearns | last post by:
When I run the following query with the two sections commented out, the response time is between 1 an 2 seconds; with the first indicated section enabled, the response goes up to 15 seconds even...
5
by: Eli | last post by:
Hi, I want to check whether a value is a scalar. A scalar can be: - None (null) - string - number (integer, float) - boolean How can I validate a value is one of these types? I care about...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
2
by: nshishir | last post by:
In oracle, there is a performance improvement if scalar subqueries are used instead of joins. Does this hold good for Db2 (8.2) too?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
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
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...

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.