[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

Unable to resolve what appears to be syntax error trying to build User Defned Function. What am I doing wrong?

Hi all,
I am getting this error when I tried to build the a User defined Function. The code and error message is as follows:
       CODE:
CREATE FUNCTION fnIsVowel
(
                                 c            CHAR(1)
)
RETURNS SMALLINT
SPECIFIC  fnIsVowel
BEGIN ATOMIC
      IF ( c = 'A' ) OR ( c = 'E' ) OR ( c = 'I' ) OR ( c = 'O' ) OR ( c = 'U' ) OR ( c = 'Y' ) THEN
                  RETURN  1;
      END IF;
      RETURN  0;
END

CREATE FUNCTION fnSlavoGermanic
(
                                 Word         CHAR(50)
)
RETURNS SMALLINT
SPECIFIC  fnSlavoGermanic
BEGIN ATOMIC
      
--Catch NULL also...
      IF ( LOCATE('W', Word) > 0 ) OR ( LOCATE('K', Word) > 0 ) OR ( LOCATE('CZ', Word) > 0 ) THEN
            RETURN  1;
      END IF;
      
                   RETURN  0;
END
  ERROR MESSAGE
:
DB2ADMIN.fnIsVowel - Build started.
Create user-defined function returns -104.
DB2ADMIN.fnIsVowel: 20: [IBM][CLI Driver][DB2/LINUX] SQL0104N  An unexpected token "CHAR" was found following "Word    ".  Expected tokens may include:  ".".  LINE NUMBER=20.  SQLSTATE=42601

DB2ADMIN.fnIsVowel - Build failed.
DB2ADMIN.fnIsVowel - Roll back completed successfully.

Can someone point out what is wrong with this statements?

Thanks
             
0
Enuda
Asked:
Enuda
  • 4
  • 2
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Enuda,

Are you trying to create both functions at once?  If so, you'll need a statement terminator after the END statement that terminates the first function.

Note that the error message indicates that the parser is still processing code as if it were in the first function.  It hasn't detected the end of the function.

Try creating them one at a time or put a terminator at the end of the function.  It's tempting to try and use the ';' character, but in this case you can't.  It is the separator within the function so you need to choose another character and of course, the client needs to be set to use this other character.  The snippet below shows one way to create both functions at once.


Good Luck,
Kent

CREATE FUNCTION fnIsVowel
(
  c          CHAR(1)
)
RETURNS SMALLINT
SPECIFIC  fnIsVowel
BEGIN ATOMIC
 
   IF ( c = 'A' ) OR ( c = 'E' ) OR ( c = 'I' ) OR ( c = 'O' ) OR ( c = 'U' ) OR ( c = 'Y' ) THEN
      RETURN  1;
   END IF;
   RETURN  0;
END@
 
CREATE FUNCTION fnSlavoGermanic
(
  Word         CHAR(50)
)
RETURNS SMALLINT
SPECIFIC  fnSlavoGermanic
BEGIN ATOMIC
      
--Catch NULL also...
  IF ( LOCATE('W', Word) > 0 ) OR ( LOCATE('K', Word) > 0 ) OR ( LOCATE('CZ', Word) > 0 ) THEN
     RETURN  1;
  END IF;
  RETURN  0;
END@
 
Assuming that you're on *nix, copy the source above into a file.
 
cat db2file | db2 -dt@
quit@
 
And of course, you can also pass the file name via the command line.

Open in new window

0
 
EnudaAuthor Commented:
Kent,
Thank you for your response...this UDF thingy is driving me nuts...but I look on the bright side, I am learning something along the way. What you have here is a T-SQL translation of a phonetic search algorithm that I am trying to convert to DB2 SQL. Oh yes, I have tried using IBM's Migration Toolkit and a trial version AdventNet SwisSQL T-SQL to DB2 converter. But neither of the product produces anything that the DB2 SP/Function Builder likes... just an all around pain in the neck this weekend...

I am going to try your recommendation see if that helps...

0
 
EnudaAuthor Commented:
Hi Kent,
I have just tried the snippet and find the same error...this time, it does not even like the first create function...

DB2ADMIN.fnIsVowel - Build started.
Create user-defined function returns -104.
DB2ADMIN.fnIsVowel: 16: [IBM][CLI Driver][DB2/LINUX] SQL0104N  An unexpected token "c  CHAR(1))     RETURNS SMALLINT     LANG" was found following "FUNCTION FNISVOWEL (".  Expected tokens may include:  "<space>".  LINE NUMBER=16.  SQLSTATE=42601

DB2ADMIN.fnIsVowel - Build failed.
DB2ADMIN.fnIsVowel - Roll back completed successfully.

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
EnudaAuthor Commented:
Hi Kent,
I have just tried the snippet using the command editor instead of the Development Center and the code compiled successfully. So, you were right, I had to change the statement termination character for it to work. So, it seems that my Development center features is not working properly. I will deal with that later. Now, on to the rest of my code...wish me luck
0
 
EnudaAuthor Commented:
The @ Statement terminator solved that problem for me...thanks.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Hi Enuda,

Good Luck.  :)

I avoid the Command Center when I can.  For some things, like creating Federated object, it's the easiest, but for most development it's clearly not.

The successor product, the Developer Workbench (released with UDB version 9) is the tool that IBM should have distributed all along.  It's an excellent product.

In the meantime, I usually use the Aqua Data Studios client for most things.  It provides a common interface for my IBM, Oracle, and MySQL databases, though it doesn't have a good Procedure Editor, either.

Again, good luck,
Kent
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now