Is it possible to build this kind of dynamic query?

Hi all!
   This is a hard one ( well i think its hard or impossible ),
I would like to build a dynamic query from the value within a table.

TableQuery
ID             value
1              'tablefoo'

Tablefoo
FirstName        LastNAme
'Mike'              'Mike'

Now i would like to build a query like
SELECT * FROM tablefoo <-- but this value has to come from a previous SELECT
so i would like to do something like
SELECT * FROM (SELECT value FROM TAbleQuery WHERE ID = 1) <-- the result of that select would be 'tablefoo'

is it possible
Chad
LVL 1
ChadakAsked:
Who is Participating?
 
rafranciscoConnect With a Mentor Commented:
DECLARE @tableName VARCHAR(100)
DECLARE @SQL  VARCHAR(2000)

SELECT @tableName = Value
FROM TableQuery
WHERE ID = 1

SET @SQL = 'SELECT * FROM ' + @tableName
EXEC (@SQL)
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.