Unix Technical Forum

error creating UDF

This is a discussion on error creating UDF within the DB2 forums, part of the Database Server Software category; --> I am using Db2 8.1 on AIX 5.1. I am trying to create a UDF for the first time. ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-26-2008, 10:01 AM
sujit
 
Posts: n/a
Default error creating UDF

I am using Db2 8.1 on AIX 5.1.

I am trying to create a UDF for the first time.
I was getting some errors, so I tried the examples from
the "SQL Reference Volume 2" manual.

This UDF gets created properly.

CREATE FUNCTION TAN
(X DOUBLE)
RETURNS DOUBLE
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC
RETURN SIN(X)/COS(X);

However, when I try to create the following UDF, I get errors
similar to the errors that I was getting with my own UDF.

CREATE FUNCTION REVERSE(INSTR VARCHAR(4000))
RETURNS VARCHAR(4000)
DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL
BEGIN ATOMIC
DECLARE REVSTR, RESTSTR VARCHAR(4000) DEFAULT '';
DECLARE LEN INT;
IF INSTR IS NULL THEN
RETURN NULL;
END IF;
SET (RESTSTR, LEN) = (INSTR, LENGTH(INSTR));
WHILE LEN > 0 DO
SET (REVSTR, RESTSTR, LEN)
= (SUBSTR(RESTSTR, 1, 1) || REVSTR,
SUBSTR(RESTSTR, 2, LEN - 1),
LEN - 1);
END WHILE;
RETURN REVSTR;
END;

Here are the error messages:
CREATE FUNCTION REVERSE(INSTR VARCHAR(4000))
RETURNS VARCHAR(4000)
DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL
BEGIN ATOMIC
DECLARE REVSTR, RESTSTR VARCHAR(4000) DEFAULT ''
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "''" was found following "ARCHAR(4000)
DEFAULT".
Expected tokens may include: "END-OF-STATEMENT". LINE NUMBER=5.
SQLSTATE=42601

DECLARE LEN INT
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "DECLARE" was found following
"BEGIN-OF-STATEMENT". Expected tokens may include: "<create_proc>".
SQLSTATE=42601

IF INSTR IS NULL THEN RETURN NULL
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "INSTR" was found following "IF ".
Expected
tokens may include: "JOIN <joined_table>". SQLSTATE=42601

END IF
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "END-OF-STATEMENT" was found following
"END IF".
Expected tokens may include: "JOIN <joined_table>". SQLSTATE=42601

SET (RESTSTR, LEN) = (INSTR, LENGTH(INSTR))
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "(" was found following "SET ".
Expected tokens
may include: "DELIMITED_TYPE_IDENTIFIER". SQLSTATE=42601

WHILE LEN > 0 DO SET (REVSTR, RESTSTR, LEN) = (SUBSTR(RESTSTR, 1, 1)
|| REVSTR, SUBSTR(RESTSTR, 2, LEN - 1), LEN - 1)
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "> 0 DO SET" was found following "WHILE
LEN ".
Expected tokens may include: "<space>". SQLSTATE=42601

END WHILE
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "END-OF-STATEMENT" was found following
"END
WHILE". Expected tokens may include: "JOIN <joined_table>".
SQLSTATE=42601

RETURN REVSTR
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "END-OF-STATEMENT" was found following
"RETURN
REVSTR". Expected tokens may include: "JOIN <joined_table>".
SQLSTATE=42601

END
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "END-OF-STATEMENT" was found following
"END".
Expected tokens may include: "JOIN <joined_table>". SQLSTATE=42601

Any help would be appreciated.

thanks,
Sujit
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-26-2008, 10:01 AM
Serge Rielau
 
Posts: n/a
Default Re: error creating UDF

You fell for the oldest trap in teh book .
To define an SQL routine (procedure, method, function) or a trigger which
contains semicolon it its body you have to chane teh default delimiter of
CLP or control-cemter.
e.g.

db2 -td%
CREATE FUNCTION ....
BEGIN ATOMIC
....;
....;
END
%

Cheers
Serge


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:12 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com