-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction_Basics
58 lines (43 loc) · 1.69 KB
/
Function_Basics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//The syntax of creating a stored function in MySQL is as follows://
DELIMITER $$
CREATE FUNCTION fun_name(fun_parameter(s))
RETURNS datatype
[NOT] {Characteristics}
fun_body;
//Now, we will create a function that returns the customer occupation based on the age using the below statement.//
DELIMITER $$
CREATE FUNCTION Customer_Occupation(
age int
)
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN
DECLARE customer_occupation VARCHAR(20);
IF age > 35 THEN
SET customer_occupation = 'Scientist';
ELSEIF (age <= 35 AND
age >= 30) THEN
SET customer_occupation = 'Engineer';
ELSEIF age < 30 THEN
SET customer_occupation = 'Actor';
END IF;
-- return the customer occupation
RETURN (customer_occupation);
END$$
DELIMITER;
//We can also see all stored functions available in the current database using the following statement://
SHOW FUNCTION STATUS WHERE db = 'mysqltestdb';
//Stored Function Call//
Now, we are going to see how stored function is called with the SQL statement. The following statement uses customer_occupation stored function to get the result:
SELECT name, age, Customer_Occupation(age)
FROM customer ORDER BY age;
//Stored Function Call in Procedure//
Here, we are going to see how this function can be called in a stored procedure. This statement creates a procedure in a database that uses Customer_Occupation() stored function.
DELIMITER $$
CREATE PROCEDURE GetCustomerDetail()
BEGIN
SELECT name, age, Customer_Occupation(age) FROM customer ORDER BY age;
END$$
DELIMITER ;
The below statement can be used to call the stored procedure:
CALL GetCustomerDetail();