views:

343

answers:

1

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

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?

+1  A: 
 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

Jader Dias