Monday, July 9, 2007

learning to use mysql functions

Usually requires to have the privilege to write functions

Simple function to add 2 numbers

SET GLOBAL log_bin_trust_function_creators = 1;

drop function if exists addnum;

delimiter $$


create function addnum(onenum int, twonum int)
returns int
NO SQL
begin
declare tot int;

set tot:=onenum+twonum;
return tot;
end$$

Function to return a random number


drop function if exists numberRand;
delimiter $$
create function numberRand()
returns int
begin
declare x int;
set x=rand()*1000;
return x;
end $$

Calling a user defined function from within a function



drop function if exists callingfnc;
delimiter $$
create function callingfnc()
returns int
begin
declare a,b int;
set a=numberRand();
set b=numberRand();
return (a+b);
end$$

No comments: