MySQL Stored Function Question note the table and columns are verified

Robert Silver
Robert Silver used Ask the Experts™
on
Can anyone tell me what is wrong with this simple stored function in MySQL?
CREATE FUNCTION lup_t1(sid int(12) ) RETURNS text DETERMINISTIC
BEGIN
        DECLARE a1 text;
        SELECT  abstract into a1 from notestbl where Id=sid;
        RETURN a1;
END;

Does MySQL 5.x not allow returning Text types? What is going on
Does MySQL 5.x not allow returning certain large text types
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hi,

Try the following:

DELIMITER //
CREATE FUNCTION lup_t1(sid int(12) ) RETURNS text DETERMINISTIC
BEGIN
        DECLARE a1 text;
      SELECT  abstract into a1 from notestbl where Id=sid;
        RETURN a1;
END
//

/peter
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
I have seen a bug report from long ago on TEXT that was too long. How large is the data?
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
I could not reproduce the old bug in 5.5 MySQL by the way. Without further information, I would have to agree with Peter that likely you just need to add the DELIMITER change from and to ;
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Robert SilverSr. Software Engineer

Author

Commented:
For the record what does DELIMITER //    {CODE} //   do?
Robert SilverSr. Software Engineer

Author

Commented:
I beleieve this is due to a semicolon confusing things so by making // the delimiter
we open and close the code in  // marks.
I believe this is explained in the Section 17.1 of the pdf file: mysql-refman-5.5-en.pdf file
reference manual found at
 http://dev.mysql.com/doc/

Would that be correct?
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
Yes. Since the semicolon is the delimiter and as such is likely to be in your code of the function, you have to change the delimiter temporarily, then change it back when you are done.

-- ; used here since we have not changed delimiter yet
USE mysql;

DELIMITER //

-- // used here since we already changed delimiter
DROP FUNCTION IF EXISTS lup_t1 //

CREATE FUNCTION lup_t1(sid int(12) ) RETURNS text DETERMINISTIC
BEGIN
        DECLARE a1 text;
        SELECT  abstract into a1 from notestbl where Id=sid;
        RETURN a1;
END
//
DELIMITER ;

Commented:
You have to show where the procedure ends, at the same time you have to show where each statement ends. So to not confuse the parser, you need to have different delimiters.
delimiter // changes the delimiter showing where the procedure ends, while the delimiter for statements stays as ;

If you have an one line procedure you don't need to change delimiter:

CREATE FUNCTION hello (s CHAR(20)) RETURNS CHAR(50) DETERMINISTIC
RETURN CONCAT('Hello, ',s,'!');

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial