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

python - Getting ESC characters and non-printables from UDP socket / buffer -

Convert excel to pdf with VBScript -