Create a table with a dynamic name


/ Published in: MySQL
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.