Saadi
Saadi

Reputation: 2237

get data from select statement with table name as a result of other query

I'm not very perfect in SQL. but I've got a scenario where I need to get data from the table which is the result of another query.

something like this:

select * from (select top 1 col from tableA)

where top 1 col contains the table name.

Sample data:

tableA:

col
tableB

tableB:

col
1

Upvotes: 0

Views: 1815

Answers (1)

NP3
NP3

Reputation: 1264

DECLARE @sql NVARCHAR(200);
SELECT TOP 1 @sql = 'SELECT * FROM ' + col FROM tableA
EXECUTE sp_executesql @sql

Upvotes: 3

Related Questions