473,765 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 getManufacturer Id
(
manufacturer VARCHAR(128)
)
RETURNS BIGINT
Language sql
modifies sql data
BEGIN ATOMIC

declare manufacturer_ro w bigint;
set manufacturer_ro w = (select manufacturer_id from manufacturer where
manufacturer_na me = manufacturer);
IF(manufacturer _row IS NULL ) THEN

set manufacturer_ro w = nextval for manufacturer_se q ;
INSERT INTO manufacturer VALUES(manufact urer_row, manufacturer);

END IF;
return manufacturer_ro w;
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 getManufacturer Id(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 5329
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 getManufacturer Id
(
manufacturer VARCHAR(128)
)
RETURNS BIGINT
Language sql
modifies sql data
BEGIN ATOMIC

declare manufacturer_ro w bigint;
set manufacturer_ro w = (select manufacturer_id from manufacturer where
manufacturer_na me = manufacturer);
IF(manufacturer _row IS NULL ) THEN

set manufacturer_ro w = nextval for manufacturer_se q ;
INSERT INTO manufacturer VALUES(manufact urer_row, manufacturer);

END IF;
return manufacturer_ro w;
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 getManufacturer Id(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(<function name>(<paramete rs>)) 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
2037
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 be a scalar. Unfortunately, $gender2 shows up as null in the database. Mountain Man
1
2310
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; use IO::Select; use IO::Handle;
7
6534
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 dooooooown. I expect slow-down, of course, and would even not be surprised at slow-downs up to a factor of, say 50:1, but I'm seeing slow-downs more like 1000:1 or 100000:1. I'm sure it would actually be faster to actually export a table, use VB to...
7
1086
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 function dbo.scalar_func go create function dbo.scalar_func() returns int
2
2787
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 specialisation for all scalar types? This is because of the large number of functions to overload. For example:
5
1682
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 though t1.bh_disposal_code IS NOT NULL in only one row; with the second section enabled, it goes up to 592 seconds, even though t1.mating is NULL in all the rows chosen. Why should the (supposedly never executed) scalar subqueries cost so much? It...
5
11887
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 the value only, and not its class methods.
0
8327
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 records there is an error "Incorrect syntax near '-'. Must declare the scalar variable "@UserName". I worked out in design view,code is automatically generated.Iam not able fix the error. Iam working with Visual Web Developer-2005 Express Edition
2
4216
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
9568
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
9398
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
10160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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
8831
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
7378
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
6649
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();...
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.