dbTalk Databases Forums  

Session Variable Question

comp.databases.mysql comp.databases.mysql


Discuss Session Variable Question in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Joe Hesse
 
Posts: n/a

Default Session Variable Question - 02-18-2011 , 09:58 AM






Hi,

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. Help would be
appreciated.

Thanks,
Joe

===================================
mysql> SET @MYTABLE='Faculty';

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM @MYTABLE;

ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right syntax
to use near '@MYTABLE' at line 1
mysql>
===================================

Reply With Quote
  #2  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: Session Variable Question - 02-18-2011 , 10:42 AM






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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.