Home > Database > Mysql Tutorial > Create temporary table in MySQL procedure?

Create temporary table in MySQL procedure?

WBOY
Release: 2023-08-27 21:09:02
forward
1513 people have browsed it

在 MySQL 过程中创建临时表?

The syntax to create a temporary table in a MySQL stored procedure is as follows:

CREATE PROCEDURE yourProcedureName()
   BEGIN
      CREATE TEMPORARY TABLE yourTemporaryTableName SELECT yourValue;
   END
Copy after login

Let us implement the above syntax to create a temporary table and insert some records in the table. Following is the query to create a stored procedure and a temporary table in it −

mysql> DELIMITER //
mysql> CREATE PROCEDURE create_Temporary_Table()
   -> BEGIN
   ->    CREATE TEMPORARY TABLE tmpDemoTable SELECT 500;
   -> END//
Query OK, 0 rows affected (0.15 sec)
Copy after login

The following is the query to insert records in the table:

mysql> CREATE PROCEDURE insert_Record_InTempTable()
   -> BEGIN
   ->    INSERT INTO tmpDemoTable VALUES (300);
   -> END//
Query OK, 0 rows affected (0.06 sec)
Copy after login

mysql> DELIMITER

Now you can call the above stored procedure to create a temporary table −

mysql> call create_Temporary_Table();
Query OK, 1 row affected (0.00 sec)

mysql> call insert_Record_InTempTable();
Query OK, 1 row affected (0.00 sec)
Copy after login

Use the select statement to display all the records in the table −

mysql> select *from tmpDemoTable;
Copy after login

Output

This will produce the following Output −

+-----+
| 500 |
+-----+
| 500 |
| 300 |
+-----+
2 rows in set (0.00 sec)
Copy after login

The above is the detailed content of Create temporary table in MySQL procedure?. 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