- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I create MySQL stored procedure with OUT parameter?
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)
- Related Articles
- How can I create MySQL stored procedure with IN parameter?
- How can I create MySQL stored procedure with INOUT parameter?
- MySQL stored-procedure: out parameter?
- What is stored procedure and how can we create MySQL stored procedures?
- How can I create a stored procedure to insert values in a MySQL table?
- How can I create a stored procedure to delete values from a MySQL table?
- How can I create a stored procedure to update values in a MySQL table?
- How can I create a MySQL stored procedure that returns multiple values from a MySQL table?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Create a stored procedure with delimiter in MySQL
- How can we create MySQL stored procedure to calculate the factorial?
- How can we invoke MySQL stored procedure?
- Create variables in MySQL stored procedure with DECLARE keyword
- How can I get all the records of a table by passing its name as the parameter of MySQL stored procedure?
- How can we alter a MySQL stored procedure?

Advertisements