To make it understand we are using the table named ‘student_info’ which have the following values −
mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | | 125 | Raman | Shimla | Computers | +------+---------+------------+------------+ 4 rows in set (0.00 sec)
Now, with the help of the following query, we will create a stored procedure with OUT parameter which will count the total of a particular subject by providing the subject name as the parameter.
mysql> DELIMITER // ; mysql> Create Procedure subjects (IN S_Subject VARCHAR(25), OUT total INT) -> BEGIN -> SELECT count(subject) -> INTO total -> FROM student_info -> WHERE subject = S_subject; -> END // Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ;
‘S-Subject’ is the IN parameter that is the number of subjects we want to count and ‘total’ is the OUT parameter that stores the number of subjects for a particular subject.
mysql> CALL subjects('Computers', @total); Query OK, 1 row affected (0.02 sec) mysql> Select @total; +--------+ | @total | +--------+ | 1 | +--------+ 1 row in set (0.01 sec) mysql> CALL subjects('History', @total); Query OK, 1 row affected (0.00 sec) mysql> Select @total; +--------+ | @total | +--------+ | 2 | +--------+ 1 row in set (0.00 sec)