title: Compare two variables in an SQL WHILE loop
body: <p>I am getting the error </p>
  
  <blockquote>
    <p>Msg 102, Level 15, State 1, Line 16<br>
    Incorrect syntax near '@PacketCode'.</p>
  </blockquote>
  
  <p>when I try to execute. Something is wrong with my WHILE loop but I can't see what. Any suggestions?</p>
  
  <pre><code>DECLARE @PacketCode as varchar(255)
  SET @PacketCode = 'ZZZ_Archive_TEST'
  
  DECLARE @Value as varchar(255)
  SET @Value = ''
  
  DECLARE @i int
  SET @i = 1
  
  SELECT 
      @Value = (SELECT packetcode
                FROM tblScriptReports
                WHERE packetcode = @PacketCode)
  
  WHILE (@PacketCode = @Value) 
  BEGIN
      @PacketCode = @PacketCode + ' (' + @i + ')'
  
      SELECT @Value = (SELECT packetcode
                      FROM tblScriptReports
                      WHERE packetcode = @PacketCode)
  
      SET @i = @i + 1                   
  END
  
  SELECT @PacketCode
  </code></pre>