1

Here is a tricky problem i am trying to solve, Having difficulty in solving it


Suppose there is a simple query::

String College="Harvard"

SELECT * FROM College
  • Above Harvard is the name of the table
  • College just has the value in it and is not the name of a table

Query will fail because system is assuming College as the name of the table and searching for it which is not there

How to solve this .... in terms of SQL statements

Hope i am clear

Thanks,

3
  • @College:= "MyTable"; SELECT * FROM @College; Commented Aug 28, 2013 at 18:33
  • @ bksi .... Please can you post as the answers .... i didnt understan the query u posted ... sorry im a newbie Commented Aug 28, 2013 at 18:40
  • @Iplay:- I guess my answer is the one which you are looking for and which bksi also means!!! :) Hope this resolves your tricky query ;) Commented Aug 28, 2013 at 18:43

1 Answer 1

2

How about trying like this :)

SET @College:='Harvard';
SET @sql_text = concat('SELECT * FROM ', @College)

PREPARE stmt FROM @sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Sign up to request clarification or add additional context in comments.

5 Comments

Should it not be SET @sql_text = concat('SELECT * FROM ', @College);
thanks .....that helps .... I posted this question to solve another problem i posted on site .... have a look at [stackoverflow.com/q/18495186/2582071] .... how can i apply there ... please give a feed back
You are welcome...I will try that question also!!! P.S. Dont forget to accept this as an answer if this helped you!!! :)
Please look into it .... even if you have not worked on express framework .... post your views on Mysql part of it , thanks
@Iplay:- And if that helps you too please dont forget to accept that as an answer too!!! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.