Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

subquery

Posted on 2005-05-17
3
Medium Priority
?
181 Views
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"

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

What am I doing wrong?


 
0
Comment
Question by:krbnldy
3 Comments
 
LVL 28

Expert Comment

by:rafrancisco
ID: 14019669
Put an alias to your sub-query:

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

Accepted Solution

by:
adathelad earned 200 total points
ID: 14019671
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
 

Author Comment

by:krbnldy
ID: 14020589
adathelad

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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

564 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