title: Must declare the scalar variable
body: <p>I wrote this SQL in a stored procedure but not working,</p>
  
  <pre><code>declare @tableName varchar(max) = 'TblTest'
  declare @col1Name varchar(max) = 'VALUE1'
  declare @col2Name varchar(max) = 'VALUE2'
  declare @value1 varchar(max)
  declare @value2 varchar(200)
  
  execute('Select TOP 1 @value1='+@col1Name+', @value2='+@col2Name+' From '+ @tableName +' Where ID = 61')
  
  select @value1
  
  execute('Select TOP 1 @value1=VALUE1, @value2=VALUE2 From TblTest Where ID = 61')
  </code></pre>
  
  <p>This SQL throws this error: </p>
  
  <blockquote>
    <p>Must declare the scalar variable "@value1".</p>
  </blockquote>
  
  <p>I am generating the SQL dynamically and I want to get value in a variable. What should I do?</p>