title: MySQL : How to Create table dynamically in stored procedure?
body: <p>I want to drop and create a table with name passed in the stored procedure argument. I've written following stored procedure.</p>
  
  <pre><code>CREATE DATABASE db1;
  USE db1;
  DROP PROCEDURE IF EXISTS drop_create;
  DELIMITER $$  
  CREATE PROCEDURE drop_create(IN tbname VARCHAR(15))
  BEGIN
  
  SET @droptable = CONCAT ("DROP TABLE IF EXISTS ", tbname);
  SET @createtable = CONCAT("CREATE TABLE ", tbname, " (c0 INT PRIMARY KEY,  c1 VARCHAR(16000)" );
  
  PREPARE deletetb FROM @droptable;
  PREPARE createtb FROM @createtable ;
  
  EXECUTE deletetb ; 
  EXECUTE createtb; 
  
  DEALLOCATE PREPARE createtb ;
  DEALLOCATE PREPARE deletetb ;
  
  END $$
  DELIMITER ;
  </code></pre>
  
  <p>When I execute it, I get following error. </p>
  
  <pre><code>Call db1.drop_create('abd');
  ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
  </code></pre>
  
  <p>Apparently, I am not able to find the syntax error in SP above. What am I doing wrong?  Problem must be in CREATE TABLE related statements because when I comment line#8, 10, 12 &amp; 13 then SP works fine. I am using Mysql 5.6</p>