subquery

I am trying to use the following to retrieve everything in a column from a table that I do not know the name but can refernce if using an id,  but I am getting an error

select * FROM
(select fldtable from tblQuestions where QuesID = '12')

the above should result in  "select * from tbltest"

==error---
 Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near ','.

What am I doing wrong?


 
krbnldyAsked:
Who is Participating?
 
adatheladCommented:
You are dynamically creating a SELECT statement, and so this cannot be done the way you have tried. You would need to use dynamic SQL like below:

DECLARE @SQL VARCHAR(1000)
SELECT @SQL = 'SELECT * FROM ' + fldtable
FROM tblQuestions
WHERE QuesID= '12'

EXECUTE (@SQL)
0
 
rafranciscoCommented:
Put an alias to your sub-query:

select * FROM
(select fldtable from tblQuestions where QuesID = '12')  a
0
 
krbnldyAuthor Commented:
adathelad

your code works great in Query analyzer,  but how do i write this on an asp page
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.