Select*fromstudent_info;+------+---------+-- ----------+----------------+|id |Name |Address |Subjec"/> Select*fromstudent_info;+------+---------+-- ----------+----------------+|id |Name |Address |Subjec">

Home  >  Article  >  Database  >  How to create a MySQL stored procedure that returns multiple values ​​from a MySQL table?

How to create a MySQL stored procedure that returns multiple values ​​from a MySQL table?

WBOY
WBOYforward
2023-08-23 12:21:03808browse

How to create a MySQL stored procedure that returns multiple values ​​from a MySQL table?

We can create a stored procedure with IN and OUT parameters to get multiple values ​​from a MySQL table. To make it understandable, let's take an example of a table named 'student_info' which contains the following data −

mysql> Select * from student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Jaipur     | Literature |
| 110  | Rahul   | Chandigarh | History    |
| 125  | Raman   | Bangalore  | Computers  |
+------+---------+------------+------------+
4 rows in set (0.01 sec)

Now, by creating the following procedure named 'select_studentinfo', we can 'The value selects the value from the 'student_info' table −

mysql> DELIMITER // ;
mysql> Create Procedure Select_studentinfo ( IN p_id INT, OUT p_name varchar(20),OUT p_address varchar(20), OUT p_subject varchar(20))
    -> BEGIN
    -> SELECT name, address, subject INTO p_name, p_address, p_subject
    -> FROM student_info
    -> WHERE id = p_id;
    -> END //
Query OK, 0 rows affected (0.03 sec)

In the above query, in addition to 1 IN parameter, there are 4 OUT parameters. Now, call the procedure with the condition value we want to provide as shown below −

mysql> DELIMITER ; //
mysql> CALL Select_studentinfo(110, @p_name, @p_address, @p_subject);
Query OK, 1 row affected (0.06 sec)

mysql> Select @p_name AS Name,@p_Address AS Address, @p_subject AS Subject;
+--------+------------+-----------+
| Name   | Address    | Subject   |
+--------+------------+-----------+
| Rahul  | Chandigarh | History   |
+--------+------------+-----------+
1 row in set (0.00 sec)

The above is the detailed content of How to create a MySQL stored procedure that returns multiple values ​​from a MySQL table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete