Home > Database > Mysql Tutorial > body text

MySQL: Insert a row and get content?

WBOY
Release: 2023-09-22 09:53:10
forward
741 people have browsed it

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

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

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

Now you can call the stored procedure:

mysql> call Insert_select('John');
Copy after login

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!