Home Table name as variable
Reply: 0

Table name as variable

user4791 Published in September 19, 2018, 3:16 am

I am trying to execute this query:

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

This produces the following error:

Msg 1087, Level 16, State 1, Line 5

Must declare the table variable "@tablename".

What's the right way to have table name populated dynamically?

share|improve this question

7 Answers 7

active oldest votes
up vote 96 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.412075 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO