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

How to call stored procedure using select statement in MySQL?

王林
王林forward
2023-08-26 21:49:141237browse

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)

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 ;

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)

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;

This will produce the following output

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

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!

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