title: SQL Dynamic Column in a loop
body: <p>I want to make the following procedure</p>
  
  <pre><code>declare @a as varchar(10)
  set @a= '1'
  declare @sql as varchar(1000)
  --select @sql ='select convert(float,'+@a+')+1' 
  declare @b as varchar(20)
  set @b = '5'
  
  
  select @sql= 'while ('+@a+'&lt;=4)
  begin
  select ['+@a+'] from #Seg1PD_TruncadaFinal where truncada ='+@a+'
  set '+@a+'='+@a+'+1 
  end'
  exec(@sql)  
  </code></pre>
  
  <p>I have a table 6x5, where 5 column names are 1,2,3,4,5 and the other is "Truncada" with 5 rows: 1,2,3,4,5. I want to select the column 1 with the row 1 for example and then other combinations, so i tried that. The problem is with when i want to change the value of @a, maybe is because it's a varchar, but this is not a problem with the condition in the while. </p>
  
  <p>Sorry, i wasnt very clear, this is the error I receive:</p>
  
  <pre><code>Msg 102, Level 15, State 1, Line 1574 Incorrect syntax near '1'.
  </code></pre>
  
  <p>But, when i write the code without de <code>set '+@a+'='+@a+'+1</code> its run ok with and infinite loop(because it never finish). So i think that the problem is in the set.</p>