Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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
0
Chadak
Asked:
Chadak
1 Solution
 
rafranciscoCommented:
DECLARE @tableName VARCHAR(100)
DECLARE @SQL  VARCHAR(2000)

SELECT @tableName = Value
FROM TableQuery
WHERE ID = 1

SET @SQL = 'SELECT * FROM ' + @tableName
EXEC (@SQL)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now