为了使其理解,我们使用名为“ student_info”的表,该表具有以下值-
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)
现在,借助以下查询,我们将创建一个带有OUT参数的存储过程,该存储过程将通过提供主题名称作为参数来计算特定主题的总数。
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 // mysql> DELIMITER ;
“ S-Subject”是IN参数,它是我们要计算的主题数,“ total”是OUT参数,用于存储特定主题的主题数。
mysql> CALL subjects('Computers', @total); mysql> Select @total; +--------+ | @total | +--------+ | 1 | +--------+ 1 row in set (0.01 sec) mysql> CALL subjects('History', @total); mysql> Select @total; +--------+ | @total | +--------+ | 2 | +--------+ 1 row in set (0.00 sec)