How to write functions in MySQL

Posted by Amritha444 under Sql Server on 1/7/2011 | Points: 10 | Views : 12500 | Status : [Member] | Replies : 3
Hi all

Can any one Paste Working simple function in MySQL.I just want to study this .

Thanks in Advance




Responses

Posted by: Madhuk on: 1/7/2011 [Member] Starter | Points: 25
Posted by: Karthikanbarasan on: 1/7/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Check this link... It shows how to add the user defined functions in MYSQL and its features setp by step

http://dev.mysql.com/doc/refman/5.5/en/adding-udf.html

Thanks
Karthik
www.f5Debug.net

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chvrsri on: 1/7/2011 [Member] [Moderator] [MVP] Silver | Points: 25

Up
0
Down

Hi Amritha44,

This is simple function in my SQL
mysql> DELIMITER |
mysql>
CREATE FUNCTION WEIGHTED_AVERAGE (n1 INT, n2 INT, n3 INT, n4 INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE avg INT;
SET avg = (n1+n2+n3*2+n4*4)/8;
RETURN avg;
END|
Query OK, 0 rows affected (0.00 sec)
mysql>

You can run your function :

SELECT WEIGHTED_AVERAGE(70,65,65,60)\G

Output is :

WEIGHTED_AVERAGE(70,65,65,60): 63
1 row in set (0.00 sec)

Refer this link for detailed explaination of this example :

http://www.databasejournal.com/features/mysql/article.php/3569846/MySQL-Stored-Functions.htm


Thanks,
Radha Srikanth

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response