Posted By

gtaking on 01/01/11


Tagged

mysql sql table name dynamic


Versions (?)

Create a table with a dynamic name


 / Published in: MySQL
 

the following code can be used to create a table with a dynamic name

  1. DECLARE id_val INT(11) ;
  2. DECLARE table_lnk VARCHAR(255) ;
  3. SELECT `<column>` INTO id_val FROM `<table>` WHERE `<condition>`;
  4. #Create the new table name
  5. SET @create_query = 'CREATE TABLE `?` (<column/s> )';
  6. SET table_lnk=CONCAT(id_val,'_table');
  7. SET @create_query =REPLACE(@create_query,'?',table_lnk);
  8. #Prepare the create table statement
  9. # preparation is necessary for dynamic table name
  10. prepare create_table_stmt FROM @create_query;
  11. execute create_table_stmt ;
  12. DEALLOCATE PREPARE create_table_stmt;

Report this snippet  

You need to login to post a comment.