title: A table name as a variable
body: <p>I am trying to execute this query:</p>
  <pre><code>declare @tablename varchar(50)
  set @tablename = 'test'
  select * from @tablename
  </code></pre>
  <p>This produces the following error:</p>
  <blockquote>
  <p>Msg 1087, Level 16, State 1, Line 5</p>
  <p>Must declare the table variable &quot;@tablename&quot;.</p>
  </blockquote>
  <p>What's the right way to have the table name populated dynamically?</p>