Joe Hesse <JoeHesse (AT) gmail (DOT) com> wrote:
Quote:
I have a table Faculty where the SQL "SELECT * FROM Faculty;" works fine.
I would like to have the table "Faculty" be retrieved from a session
variable. I tried the following and it didn't work.
mysql> SET @MYTABLE='Faculty';
mysql> SELECT * FROM @MYTABLE;
ERROR 1064 (42000): You have an error in your SQL syntax |
This is called "dynamic SQL". MySQL supports that with the PREPARE and
EXECUTE statements:
SET @MYTABLE='Faculty';
....
SET @querystring=CONCAT('SELECT * FROM ', @MYTABLE);
PREPARE stmt1 FROM @querystring;
EXECUTE stmt1;
http://dev.mysql.com/doc/refman/5.1/...tatements.html
XL