MySQL Functions
By Prof. B.A.Khivsara
Note: The material to prepare this presentation has been taken from internet and are
generated only for students reference and not for commercial use.
What's a Stored Function
If procedural programming is new to you, you may be wondering
what the difference is between a Stored Procedure and a Stored
Function. Not too much really.
A function always returns a result, and can be called inside an SQL
statement just like ordinary SQL functions.
A function parameter is the equivalent of the IN procedure
parameter, as functions use the RETURN keyword to determine
what is passed back.
Stored functions also have slightly more limitations in what SQL
statements they can run than stored procedures.
Stored Function- Syntax
• CREATE FUNCTION function_name(param1,param2,…)
• RETURNS datatype
• [NOT] DETERMINISTIC
• Begin
• Declarations
• Statements
• End
Stored Function- Syntax Explanation
First, you specify the name of the stored function
after CREATE FUNCTION clause.
Second, you list all parameters of the stored function
inside the parentheses.
By default, all parameters are IN parameters. You cannot
specify IN , OUT or INOUT modifiers to the parameters.
Third, you must specify the data type of the return value in
the RETURNS statement.
Stored Function- Syntax Explanation
Fourth, for the same input parameters, if the stored
function returns the same result, it is considered
deterministic and otherwise the stored function is not
deterministic.
Fifth, you write the code in the body of the stored
function. Inside the body section, you have to specify at
least one RETURN statement. The RETURN statement
returns a value to the caller. Whenever the RETURN
statement is reached, the stored function’s execution is
terminated immediately.
MySQL stored function – Example 1
MySQL> Delimiter // To set the delimiter
MySQL>Create function Add1(a int, b int) returns int
Begin
Declare int;
Set c= a+b;
Return c;
End;
//
MySQL> Select Add1(10,20) ; To run the Function
MySQL stored function – Example 2
MySQL> Delimiter // To set the delimiter
MySQL>Create function Max1 (n1 Int,n2 Int) Returns Varchar(10)
DETERMINISTIC
BEGIN
DECLARE Maximum1 varchar(10);
If n1>n2 then
Set Maximum1= ‘Maximum number is n1’ ;
Else
Set Maximum1= ‘Maximum number is n2’;
End if;
Return Maximum;
End;
//
MySQL> Select Max1(10,20) ; To run the Function
MySQL stored function – Example 3
MySQL> Delimiter // To set the delimiter
MySQL>Create function Rname(rno1 int) returns varchar(20)
Begin
Declare sname varchar(20);
Select name into sname from Stud where rno=rno1;
Return sname;
End;
//
MySQL> Select Rname(1) ; To run the Function
MySQL stored function – Example 4
MySQL> Delimiter // To set the delimiter
MySQL>Create function Fgrade(rno1 int) returns varchar(20)
DETERMINISTIC
Begin
Declare grade varchar(20);
Declare mark1 int;
Select mark into mark1 from Stud where rno=rno1;
If (mark1 >75) then
Set grade= ‘Distinction’
ElseIf (mark1>=60 and mark1<75) then
Set grade=‘First Class’
ElseIf (marks <60) then
Set grade=‘Pass Class’
End if;
Return grade;
End;
//
MySQL> Select Fgrade(1) ; To run the Function
Assignment
Write a Stored Procedure/Function
namely proc_Grade for the Schema Required are:
categorization of student.
If marks scored by students in
Stud_Marks(name,
examination is <=1500 and
total_marks)
marks>=990 then distinction category
If marks scored are between 989
and900 category is first class, Result(Roll,Name, Class)
If marks 899 and 825 category is
Higher Second Class
References
• http://www.databasejournal.com/features/mysql/article.php/
3569846/MySQL-Stored-Functions.htm
• http://www.mysqltutorial.org/mysql-stored-function/
• https://dev.mysql.com/doc/refman/5.7/en/create-
procedure.html