sql - Table name as variable -


i trying execute query:

declare @tablename varchar(50) set @tablename = 'test' select * @tablename 

this produces following error:

msg 1087, level 16, state 1, line 5

must declare table variable "@tablename".

what's right way have table name populated dynamically?

table names , column names need static, if query static. dynamic table or column names, should generate full sql dynamically, , use sp_executesql execute it.

more details here: the curse , blessings of dynamic sql


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

Qt QGraphicsScene is not accessable from QGraphicsView (on Qt 5.6.1) -

What is happening when Matlab is starting a "parallel pool"? -