Posted on 2005-05-17
Last Modified: 2010-03-19
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"

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

What am I doing wrong?

Question by:krbnldy
    LVL 28

    Expert Comment

    Put an alias to your sub-query:

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

    Accepted Solution

    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:

    SELECT @SQL = 'SELECT * FROM ' + fldtable
    FROM tblQuestions
    WHERE QuesID= '12'


    Author Comment


    your code works great in Query analyzer,  but how do i write this on an asp page

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now