Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-08-23 12:21:03
forward
807 people have browsed it

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)
Copy after login

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template