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. ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 |
| ||||
| 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 |