Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Stored Function MySQL

Uploaded by

motoholic10
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Stored Function MySQL

Uploaded by

motoholic10
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

mysql> use mydatabase;

mysql> select * from batch;


+---------+----------+--------------------+-----------+-------+----------+
| stud_id | batch_id | bname | course | marks | city |
+---------+----------+--------------------+-----------+-------+----------+
| 100 | 1000 | Fullstack-b1-ol-22 | Fullstack | 89 | Banglore |
| 101 | 1001 | Java-b1-ol-22 | Java | 89 | Vashi |
| 110 | 1010 | PHP-b1-ol-22 | PHp | 82 | NULL |
+---------+----------+--------------------+-----------+-------+----------+
3 rows in set (0.05 sec)

mysql> delimiter //
mysql> create function ResultMarks(mark int)
-> returns varchar(20)
-> deterministic
-> begin
-> declare results varchar(20);
-> if(mark>90) then
-> set results='Outstanding';
-> elseif(mark>80 and mark<90) then
-> set results='Excellent';
-> elseif(mark>70 and mark<80) then
-> set results='Very Good';
-> else
-> set results='Good';
-> end if;
-> return results;
-> end//
Query OK, 0 rows affected (0.05 sec)

mysql> select marks,ResultMarks(marks) from batch;


-> //
+-------+--------------------+
| marks | ResultMarks(marks) |
+-------+--------------------+
| 89 | Excellent |
| 89 | Excellent |
| 82 | Excellent |
+-------+--------------------+
3 rows in set (0.01 sec)

mysql> create table emp


-> (
-> eid int,
-> ename varchar(20),
-> salary int
-> );
-> //
Query OK, 0 rows affected (0.04 sec)

mysql> insert into emp values(100,'Anup',25000)//


Query OK, 1 row affected (0.01 sec)
mysql> commit//
Query OK, 0 rows affected (0.00 sec)

mysql> create function tax_calculate(sal int)


-> returns decimal(9,2)
-> deterministic
-> begin
-> return (sal*0.1);
-> end//
Query OK, 0 rows affected (0.01 sec)

mysql> select salary,tax_calculate(salary) from emp;


-> //
+--------+-----------------------+
| salary | tax_calculate(salary) |
+--------+-----------------------+
| 25000 | 2500.00 |
| 35000 | 3500.00 |
| 15000 | 1500.00 |
| 5000 | 500.00 |
+--------+-----------------------+
4 rows in set (0.00 sec)

mysql> create function weighted_average(n1 int,n2 int,n3 int,n4 int)


-> returns int
-> deterministic
-> begin
-> declare averages int;
-> set averages=(n1+n2+n3+n4)/4;
-> return averages;
-> end//
Query OK, 0 rows affected (0.01 sec)

mysql> select weighted_average(10,20,30,40) //


+-------------------------------+
| weighted_average(10,20,30,40) |
+-------------------------------+
| 25 |
+-------------------------------+
1 row in set (0.00 sec)

mysql> select * from emp//


+------+--------+--------+
| eid | ename | salary |
+------+--------+--------+
| 100 | Anup | 25000 |
| 101 | Vishal | 35000 |
| 102 | Suresh | 15000 |
| 103 | Amit | 5000 |
+------+--------+--------+
4 rows in set (0.00 sec)

mysql> select salary,tax_calculate(salary) from emp//


+--------+-----------------------+
| salary | tax_calculate(salary) |
+--------+-----------------------+
| 25000 | 2500.00 |
| 35000 | 3500.00 |
| 15000 | 1500.00 |
| 5000 | 500.00 |
+--------+-----------------------+
4 rows in set (0.00 sec)

mysql> create procedure sp_tax_proc()


-> begin
-> select salary,tax_calculate(salary) from emp;
-> end//
Query OK, 0 rows affected (0.01 sec)

mysql> call sp_tax_proc() //


+--------+-----------------------+
| salary | tax_calculate(salary) |
+--------+-----------------------+
| 25000 | 2500.00 |
| 35000 | 3500.00 |
| 15000 | 1500.00 |
| 5000 | 500.00 |
+--------+-----------------------+
4 rows in set (0.01 sec)

Query OK, 0 rows affected (0.03 sec)

mysql>

You might also like