CREATE FUNCTION function_name(param1,param2,…)
RETURNS datatype
[NOT] DETERMINISTIC
statements
First you can define name of stored function after create function clause.
List all parameters with in parenthesis. By default all parameters are IN parameters. You cannot define IN, INOUT, OUT modifiers to parameters.
You must specify the return data types in the return statements.
Example:
DELIMITER $$
CREATE FUNCTION CustomerLevel(p_creditLimit double) RETURNS VARCHAR(10)
DETERMINISTIC
BEGIN
DECLARE lvl varchar(10);
IF p_creditLimit > 50000 THEN
SET lvl = 'PLATINUM';
ELSEIF (p_creditLimit <= 50000 AND p_creditLimit >= 10000) THEN
SET lvl = 'GOLD';
ELSEIF p_creditLimit < 10000 THEN
SET lvl = 'SILVER';
END IF;
RETURN (lvl);
END
Now we call CustomerLevel function in any sql function.
SELECT
customerName, CustomerLevel(creditLimit)
FROM
customers
ORDER BY customerName;
We can use this stored function with in stored procedures also.
0 comments:
Post a Comment