MySQL 如何创建带有OUT参数的MySQL存储过程?
为使其理解,我们使用名为“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 //
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
‘S-Subject’是IN参数,即我们要计算的科目数,而’total’是OUT参数,它存储特定科目的科目数。
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)
阅读更多:MySQL 教程