MySQL, CREATE FUNCTION Syntax

The general syntax of Creating a Function is :

 CREATE FUNCTION func_name ([func_parameter[,...]]) RETURNS type routine_body

func_name : Function name
func_parameter : param_name type
type : Any valid MySQL datatype
routine_body : Valid SQL procedure statement

 The RETURN clause is mandatory for FUNCTION . It used to indicate the return type of function.

Now we are describing you a simple example a function. This function take a parameter and it is used to perform an operation by using an SQL function and return the result. In this example there is no need to use delimiter because it contains no internal ; statement delimiters. Example :

CREATE FUNCTION func(str CHAR(20))
RETURNS CHAR(50)
RETURN CONCAT('WELCOME TO, ',str,'!');

Query OK, 0 rows affected (0.00 sec)

 SELECT func('Galhano.com');

 

+------------------------+
| func('Galhano.com')      |
+------------------------+
| WELCOME TO, Galhano.com |
+------------------------+
1 row in set (0.00 sec)

http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html

http://www.roseindia.net/mysql/mysql5/stored-procedures-and-functions.shtml

Deixe uma resposta

O seu endereço de email não será publicado. Campos obrigatórios marcados com *