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

angular - DownloadURL return null in below code -

python 2.7 - Given three nested dictionaries, sort the top two nested dictionaries from a value in the innermost dictionary? -