Reputation: 1696
It is possible?
DECLARE @vTableName varchar(50)
SET @vTableName = (SELECT TableName FROM qms_Types WHERE Id = 1)
SELECT * FROM @vTableName
I have this error:
Msg 1087, Level 16, State 1, Line 3 Must declare the table variable "@vTableName".
Upvotes: 12
Views: 82213
Reputation: 139
It seems as though different folks are interpreting the OP differently.
I'm pretty sure the OP is asking for this type of concept / ability / maneuver...
"Put a table name into a variable and then use that variable as though it were a table name."
DECLARE @TableIWantRecordsFrom varchar(50)
-- ^^^^^^^^^^^^^^^^^^^^^^
SET @TableIWantRecordsFrom = (SELECT TableName FROM qms_Types WHERE Id = 1) -- (L1)
-- ^^^^^^^^^^^^^^^^^^^^^^
-- Let's say, at this point, @TableIWantRecordsFrom ... contains the text 'Person'
-- ^^^^^^^^^^^^^^^^^^^^^^
-- assuming that is the case then...
-- these two queries are supposed to return the same results:
SELECT top 3 fname,lname,mi,department,floor FROM Person
-- ^^^^^^
SELECT top 3 fname,lname,mi,department,floor FROM @TableIWantRecordsFrom -- (L2)
-- ^^^^^^^^^^^^^^^^^^^^^^
From reading all the responses and answers, it appears that this kind of maneuver can't be done - unless - you use dynamic SQL which...
================================================================
There are other languages where this can be done... in literally, two lines of code (see (L1) and (L2) in above code) and not having to do a lot of formatting and editing.)
(I've done it before - there is another language where all you'd need is L1 and L2...)
================================================================
It is unfortunate that SQL Server will not do this without going to a decent amount of effort...
Upvotes: 3
Reputation: 141
my solution for this:
EXECUTE('SELECT * FROM ' + TableName + '')
Upvotes: 2
Reputation: 7683
if you're trying to select from a table of that name, then you can do something like this:
DECLARE @vTableName varchar(50)
SET @vTableName = (SELECT TableName FROM qms_Types WHERE Id = 1)
EXECUTE('SELECT * FROM [' + @vTableName + ']')
Upvotes: 5
Reputation: 247860
The only way you can do this is through Dynamic SQL which refers to the practice of creating a T-SQL text and executing it using the sp_executesql
(or simply exec
)
Here is a helpful link about dynamic sql The Curse and Blessings of Dynamic SQL.
You should really think whether or not this is a case for dynamic sql or if there is another way for you to perform this operation.
Upvotes: 1
Reputation: 36166
I think you want this:
DECLARE @vTableName table(TableName varchar(50))
insert into @vTableName
SELECT TableName FROM qms_Types WHERE Id = 1
SELECT * FROM @vTableName
Upvotes: 1
Reputation: 1325
Short answer: No.
Long answer: Noooooooooooooooooooooooooooooooooooooo. Use dynamic SQL if you have to, but if you're structuring your tables in a way where you don't know the table name ahead of time, it might benefit you to rethink your schema.
Here is a great resource for learning how to use dynamic SQL: The Curse and Blessings of Dynamic SQL
Upvotes: 22