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 -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -