473,395 Members | 1,383 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.

user defined function

PDN
Sorry for my bad english, I am french speaking
...
I have been tasked to write a UDF in order to return the maximum value
of an integer column in a given table and increment it by 1.
I have no experience of UDF so I have tried this :
CREATE FUNCTION S123456T ()
RETURNS INTEGER
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN
DECLARE SEQNO INTEGER;
SELECT MAX(ID) FROM CTIR006.TSTIDNTY INTO SEQNO
RETURN SEQNO
END
;
and I get the following SQL error

CREATE FUNCTION S123456T ()
RETURNS INTEGER
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN
DECLARE SEQNO INTEGER;
---------+---------+---------+---------+---------+---------+---------+------
DSNT408I SQLCODE = -199, ERROR: ILLEGAL USE OF KEYWORD DECLARE, TOKEN
ON
<INTEGER> WAS EXPECTED
DSNT418I SQLSTATE = 42601 SQLSTATE RETURN CODE
DSNT415I SQLERRP = DSNHPARS SQL PROCEDURE DETECTING ERROR
DSNT416I SQLERRD = 0 0 0 -1 367 0 SQL DIAGNOSTIC INFORMATION
DSNT416I SQLERRD = X'00000000' X'00000000' X'00000000'
X'FFFFFFFF'
X'0000016F' X'00000000' SQL DIAGNOSTIC INFORMATION

How should I write the body to correct this ? Could someone help me ?
Thanks in advance.
PDN
Nov 12 '05 #1
2 6501
PDN wrote:
Sorry for my bad english, I am french speaking
..
I have been tasked to write a UDF in order to return the maximum value
of an integer column in a given table and increment it by 1.
I have no experience of UDF so I have tried this :
CREATE FUNCTION S123456T ()
RETURNS INTEGER
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN
DECLARE SEQNO INTEGER;
SELECT MAX(ID) FROM CTIR006.TSTIDNTY INTO SEQNO
RETURN SEQNO
END
;
and I get the following SQL error

CREATE FUNCTION S123456T ()
RETURNS INTEGER
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN
DECLARE SEQNO INTEGER;
---------+---------+---------+---------+---------+---------+---------+------ DSNT408I SQLCODE = -199, ERROR: ILLEGAL USE OF KEYWORD DECLARE, TOKEN
ON
<INTEGER> WAS EXPECTED


I don't know exactly why you got this error (I assume you are on DB2 for zOS
or so?), but you should be able to rewrite the function body like this:

CREATE FUNCTION S123456T ()
RETURNS INTEGER
NO EXTERNAL ACTION
LANGUAGE SQL
RETURN SELECT MAX(ID) + 1
FROM FROM CTIR006.TSTIDNTY;

That might also give the DB2 optimizer a better chance to optimize the
statement where the function is used as opposed to the compound statement
you had.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #2

"Knut Stolze" <st****@de.ibm.com> wrote in message
news:ch**********@fsuj29.rz.uni-jena.de...
PDN wrote:
Sorry for my bad english, I am french speaking
..
I have been tasked to write a UDF in order to return the maximum value
of an integer column in a given table and increment it by 1.
I have no experience of UDF so I have tried this :
CREATE FUNCTION S123456T ()
RETURNS INTEGER
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN
DECLARE SEQNO INTEGER;
SELECT MAX(ID) FROM CTIR006.TSTIDNTY INTO SEQNO
RETURN SEQNO
END
;
and I get the following SQL error

CREATE FUNCTION S123456T ()
RETURNS INTEGER
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN
DECLARE SEQNO INTEGER;
---------+---------+---------+---------+---------+---------+---------+----

--
DSNT408I SQLCODE = -199, ERROR: ILLEGAL USE OF KEYWORD DECLARE, TOKEN
ON
<INTEGER> WAS EXPECTED


I don't know exactly why you got this error (I assume you are on DB2 for

zOS or so?), but you should be able to rewrite the function body like this:

CREATE FUNCTION S123456T ()
RETURNS INTEGER
NO EXTERNAL ACTION
LANGUAGE SQL
RETURN SELECT MAX(ID) + 1
FROM FROM CTIR006.TSTIDNTY;
I think Knut made a typo here: I think the preceding line will work better
with only one 'FROM' in it ;-)
That might also give the DB2 optimizer a better chance to optimize the
statement where the function is used as opposed to the compound statement
you had.

Rhino
Nov 12 '05 #3

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

Similar topics

8
by: markus | last post by:
This string does not work in php: $sql="UPDATE mytable SET myfield=myuserfunction(myfield)" What I want is my function to evaluate each field and put the new value in the field. For...
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. ...
6
by: Ken Post | last post by:
Hi All: I've read a whole slew of posts about creating temp tables using stored proceedures to get the crosstab ability, but I'm wondering if, for this specific case, there might be a more...
13
by: Maroon | last post by:
Hi, I want to write a standard java user defined function, like this example:- select db_fun("roll"), roll from student; **db_fun() will work for all tables of the all databse here db_fun is...
5
by: Ed Havelaar | last post by:
I have a cool function that I want to use as a default value for a column in my table. But I can't because apparently Access doesn't allow user defined functions in expressions for default values....
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,...
3
by: chreo | last post by:
I have user-defined function in MSSQL which returns Table (with 10 columns) (sorry for Polish names) CREATE FUNCTION PACZKI_Z_AKCJI (@AKCJA_ID int) RETURNS TABLE RETURN SELECT TOP 100...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
1
by: The Man with no name | last post by:
Hi, Can we create an array of user defined dimensions in the following way:? 1 Create a class( say CONSTANT) whose lone datamember is an integer which is called say num. 2 In the public section...
6
by: JLupear | last post by:
I have written a code (a translation of my first one) that is not working. I did a user defined function that is used on 'onsubmit'. It should total the value from the form selections and...
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
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?
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.