简体   繁体   English

如何在MySQL程序中创建唯一的临时表?

[英]How to create unique temporary tables in MySQL procedures?

I was creating a temporary table in my procedure, but I always got an error "table already exists". 我在我的程序中创建了一个临时表,但我总是得到一个错误“表已经存在”。

Then I tried to create a random name to avoid collision but I don't know enough about how to execute SQL strings 然后我尝试创建一个随机名称以避免冲突,但我不知道如何执行SQL字符串

SET @tbName = CONCAT('temp', random_id);

PREPARE stmt1 FROM 'CREATE TEMPORARY TABLE ? (`FIELDNAME` float NOT NULL);';
EXECUTE stmt1 using @tbName;
DEALLOCATE PREPARE stmt1;

The code above doesn't works. 上面的代码不起作用。 Why? 为什么? How to correct it? 怎么纠正呢?

 mysql> set @table_name := 'mytable';
 Query OK, 0 rows affected (0.02 sec)

 mysql> set @sql_text:=concat('create table ',@table_name,'(id int unsigned)');
 Query OK, 0 rows affected (0.00 sec)

from http://rpbouman.blogspot.com/2005/11/mysql-5-prepared-statement-syntax-and.html 来自http://rpbouman.blogspot.com/2005/11/mysql-5-prepared-statement-syntax-and.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM