Home  >  Article  >  Database  >  MySQL: Insert a row and get content?

MySQL: Insert a row and get content?

WBOY
WBOYforward
2023-09-22 09:53:10751browse

MySQL: Insert a row and get content?

In order to insert a row and get the content, you need to use a stored procedure. First, you need to create a table. After that you need to create a stored procedure that will insert a row and get the content to the end user.

To perform the above tasks, let us first create a table. The query to create the table is as follows:

mysql> create table InsertRecord_SelectTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (1.45 sec)

Now create a stored procedure to insert a record in the above table and return the result from the table immediately after calling the stored procedure. The query to create a stored procedure is as follows:

mysql> DELIMITER //
mysql> create procedure Insert_select
   -> (
   -> In tempName varchar(40)
   -> )
   -> begin
   -> declare tempId int unsigned;
   -> insert into InsertRecord_SelectTable(Name) values (tempName);
   -> set tempId = last_insert_id();
   -> select *from InsertRecord_SelectTable where Id= tempId;
   -> END //
Query OK, 0 rows affected (0.21 sec)
mysql> DELIMITER ;

Call the stored procedure to view, insert a row and obtain the content. The query to call the stored procedure is as follows:

CALL yourStoredProcedureName;

Now you can call the stored procedure:

mysql> call Insert_select('John');

Here is the output:

+----+------+
| Id | Name |
+----+------+
| 1  | John |
+----+------+
1 row in set (0.12 sec)
Query OK, 0 rows affected, 1 warning (0.13 sec)

The above is the detailed content of MySQL: Insert a row and get content?. 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