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 -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -