MySQL Forums
Forum List  »  InnoDB

Re: Is it possible to create table using local variable?
Posted by: Aftab Khan
Date: November 27, 2012 04:52AM

>Is it possible to create table using local variable?
yes, here is quick example:

mysql> set @table='testing';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @sql=concat('create table ',@table, ' (a int, b int)');
Query OK, 0 rows affected (0.00 sec)

mysql> select @sql;
+-------------------------------------+
| @sql                                |
+-------------------------------------+
| create table testing (a int, b int) |
+-------------------------------------+
1 row in set (0.00 sec)

mysql> prepare stmt from @sql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> execute stmt;
Query OK, 0 rows affected (0.14 sec)

mysql> show create table testing\G
*************************** 1. row ***************************
       Table: testing
Create Table: CREATE TABLE `testing` (
  `a` int(11) default NULL,
  `b` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.00 sec)


Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Is it possible to create table using local variable?
1103
November 27, 2012 04:52AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.