title: Use dynamic table name in sql server query
body: <p>I have an <code>Error</code> table which stores the names of the <strong>table</strong> in which the error occurred.
  Now I want to query the table using the table name selected from the "Error" table.</p>
  
  <p>I tried to store the table name in a variable and use this variable in the FROM clause in my query. But this doesn't work:</p>
  
  <pre><code>DECLARE @tableName VARCHAR(15)
  
  select @tableName = TableName from SyncErrorRecords where Status = 'Unsynced'
  
  select * from @tableName
  </code></pre>
  
  <p>Can anyone help me out on this.
  Thanks in advance.</p>