tags:

views:

239

answers:

1

Is there any way in MySQL to put the name of the database into a variable? For example, when I have a database called 'db1', can I do something like this:

set @db= 'db1';
select * from @db.mytable;

EDIT: There is another example of what I want to do:

set @dbfrom= 'db1';
set @dbto= 'db2';
insert into @dbto.mytable (col1,col2,col3) select col2,col1,col3 from @dbfrom.mytable;
+2  A: 

With considerable effort, yes.

SET @db = 'db1';
SET @q = CONCAT('SELECT * FROM ', @db, '.mycol');
PREPARE stmt FROM @q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
chaos
ya.. it works, but I have much large queries.. :(
grilix
Sorry. That's as good as it gets.
chaos
Can I do inserts with this ?
grilix
Yeah, inserts are fine.
chaos
well.. I think its the only workaround.. thank you.. :P
grilix