Saturday, March 14, 2009

MySQL Stored Function

I have not touched any database code for about 1 year, and I have tried to help my friend with CREATE FUNCTION syntax. I have spent a couple hours with it in phpMyAdmin; however, I reviewed it very well which makes me much more clearer. Have a look the following code:


CREATE FUNCTION myFunction()
RETURNS int(11)
BEGIN
RETURN 1;
END;


There is a problem with the above code because it doesn't use any DELIMITER statements at all. I found this solution on http://forums.mysql.com/read.php?98,212863,212877#msg-212877

Well, I have tested that again on phpMyAdmin, but it still doesn't work. I found some people said don't use phpMyAdmin to run this type of statement. Hence, I tried MySQL Query Browser, it works well.


DELIMITER $$

CREATE FUNCTION myFunction() RETURNS int(11)
BEGIN
RETURN 1;
END $$

DELIMITER ;


Have a look on full syntax of create stored routine.

No comments:

Subscribe in a Reader