Home > Database > Mysql Tutorial > How to call stored procedure using select statement in MySQL?

How to call stored procedure using select statement in MySQL?

王林
Release: 2023-08-26 21:49:14
forward
1240 people have browsed it

How to call stored procedure using select statement in MySQL?

In MySQL, select from procedure cannot be used in the FROM clause. You can use the CALL command and then you can execute a SELECT statement.

Let us first create a table:

mysql> create table DemoTable2
   -> (
   -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> CustomerName varchar(100),
   -> ShippingDateTime datetime
   -> );
Query OK, 0 rows affected (0.66 sec)
Copy after login

The following is the query to create the stored procedure:

mysql> DELIMITER //
mysql> CREATE PROCEDURE insert_information(Name varchar(100),shippingtime datetime)
   -> BEGIN
   ->
   -> INSERT INTO DemoTable2(CustomerName,ShippingDateTime) VALUES(Name,shippingtime);
   -> END
   -> //
Query OK, 0 rows affected (0.16 sec)

mysql> DELIMITER ;
Copy after login

Now you can call the stored procedure using the call command:

mysql> call insert_information('Chris',NOW());
Query OK, 1 row affected, 1 warning (0.15 sec)
Copy after login

The following is a query that uses the select statement to display records in the table after calling the stored procedure.

mysql> select *from DemoTable2;
Copy after login

This will produce the following output

+------------+--------------+---------------------+
| CustomerId | CustomerName | ShippingDateTime    |
+------------+--------------+---------------------+
| 1          | Chris        | 2019-04-08 15:03:07 |
+------------+--------------+---------------------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of How to call stored procedure using select statement in MySQL?. 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