Dynamic Schema Creation and Switching Am creating a schema dynamically like:

SELECT @xxx:=LAST_INSERT_ID();

SET @schemaname = concat('CREATE SCHEMA ','TrialDB_','',@xxx);


So once after creating the Schema I would like to dump some tables into that schema. So for switching the schema I am trying:

use @schemaname ; // Which is not Working 

//Also tried the below scenario which is not working 

SET @useSchema =concat('USE','TrialDB_','',@cloudclientid);

SELECT @useSchema ;

PREPARE statement1 from @useSchema;

EXECUTE statement1;


After dumping the tables my regular flow needs to continue.
First one is how to create database dynamically.

set @db_name:= 'db_name';
set @statment = CONCAT('CREATE DATABASE ', @db_name);
prepare stmt_create FROM @statment;
execute stmt_create;
deallocate PREPARE stmt_create;


Now you want to dump some table into it.

set @db_name="db_name";
set @start_syntax="CREATE TABLE ";
set @table_syntax=".`table_name` (id int(4)) ENGINE = InnoDB;";
set @stat_execute=concat(@start_syntax,@db_name,@table_syntax);
prepare stat_ment from @stat_execute;
execute stat_ment;
deallocate prepare stat_ment;


In this way you can create database and dump the table.