在 MySQL 中创建表变量

2021-11-20 00:00:00 variables mysql database-table

我需要一个表变量来存储 MySQL 过程中表中的特定行.例如.声明@tb 表(id int,name varchar(200))

I need a table variable to store the particular rows from the table within the MySQL procedure. E.g. declare @tb table (id int,name varchar(200))

这可能吗?如果是,如何?

Is this possible? If yes how?

推荐答案

它们在 MySQL 中不存在,是吗?只需使用临时表:

They don't exist in MySQL do they? Just use a temp table:

CREATE PROCEDURE my_proc () BEGIN 

CREATE TEMPORARY TABLE TempTable (myid int, myfield varchar(100)); 
INSERT INTO TempTable SELECT tblid, tblfield FROM Table1; 

/* Do some more stuff .... */

来自 MySQL 在这里

"您可以使用 TEMPORARY 关键字创建表时.一个临时的表仅对当前可见连接,并被丢弃连接成功时自动关闭.这意味着两个不同的连接可以使用相同的临时表名不与彼此或与现有的同名的非临时表.(现有表被隐藏,直到临时表被删除.)"

"You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.)"

相关文章