SQL Server 2008 SELECT * FROM @variable?

sql-server-2008, variables

Solution

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

Problem

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".

Original source