How can I create MySQL stored procedure with IN parameter?

MySQLMySQLi Database

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 IN parameter which will show all the details of a particular student by providing the name as the parameter.

mysql> DELIMITER // ;
mysql> Create PROCEDURE detail(IN S_Name VARCHAR(20))
    -> BEGIN
    -> SELECT * From Student_info WHERE Name = S_Name;
    -> END //
Query OK, 0 rows affected (0.16 sec)
mysql> DELIMITER ;

S_Name is the IN parameter of the stored procedure ‘detail’. If we want to see all the details of the student name ‘Gaurav’ then it can be done with the help of the following query −

mysql> CALL detail('Gaurav');
+-----+--------+---------+------------+
| id  | Name   | Address | Subject    |
+-----+--------+---------+------------+
| 105 | Gaurav | Jaipur  | Literature |
+-----+--------+---------+------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> CALL detail('Raman');
+-----+-------+---------+-----------+
| id  | Name  | Address | Subject   |
+-----+-------+---------+-----------+
| 125 | Raman | Shimla  | Computers |
+-----+-------+---------+-----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)
raja
Published on 21-Feb-2018 07:52:49
Advertisements