-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoredFunction.sql
91 lines (79 loc) · 2.15 KB
/
StoredFunction.sql
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
/*
Objective: Create a Fucntion To Know the Credit class of a cutomer
Based on Some Conditions
Author Name: Swapil Jadhav
Rno:67
PRN Number: UCS21M1067
Batch: AS4
*/
Delimiter $$
create function display(credit decimal(10,2)) Returns varchar(12) Deterministic
Begin
Declare cl varchar(12);
if credit > 50000 Then
set cl='Platinum';
elseif (credit <= 50000 and credit >=30000) Then
set cl='Gold';
elseif credit < 30000 Then
set cl='Silver';
end if;
Return (cl);
end $$
Delimiter ;
/* Output
mysql> show tables;
+------------------+
| Tables_in_sample |
+------------------+
| books_fine |
| customer |
| emp |
| emp_dump |
| student_books |
+------------------+
5 rows in set (0.02 sec)
mysql> desc customer;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| cid | int | NO | PRI | NULL | |
| cname | varchar(25) | YES | | NULL | |
| camount | decimal(10,2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> select * from customer;
+-----+--------+----------+
| cid | cname | camount |
+-----+--------+----------+
| 1 | sai | 34983.23 |
| 2 | Ram | 64983.23 |
| 3 | Rahul | 44983.23 |
| 4 | Rakesh | 24983.23 |
+-----+--------+----------+
4 rows in set (0.03 sec)
mysql> select cid,display(camount);
ERROR 1054 (42S22): Unknown column 'cid' in 'field list'
mysql> select cid,display(camount) from customer;
+-----+------------------+
| cid | display(camount) |
+-----+------------------+
| 1 | Gold |
| 2 | Platinum |
| 3 | Gold |
| 4 | Silver |
+-----+------------------+
4 rows in set (0.00 sec)
mysql> create view cr_level(cid,level) as select cid,display(camount) from customer;
Query OK, 0 rows affected (0.19 sec)
mysql> select * from cr_level;
+-----+----------+
| cid | level |
+-----+----------+
| 1 | Gold |
| 2 | Platinum |
| 3 | Gold |
| 4 | Silver |
+-----+----------+
4 rows in set (0.00 sec)
*/