在 MySQL 过程中创建临时表?

在 MySQL 过程中创建临时表?

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

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)

登录后复制

以下是在表中插入记录的查询:

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

登录后复制

mysql> DELIMITER

现在您可以调用上述存储过程来创建一个临时表 −

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)

登录后复制

使用select语句显示表中的所有记录 −

mysql> select *from tmpDemoTable;

登录后复制

输出

这将产生以下输出 −

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

登录后复制

以上就是在 MySQL 过程中创建临时表?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!