title: SQL Server : error "Must Declare the Scalar Variable"
body: <p>Trying to insert into a table from other two tables with a loop</p>
  
  <pre><code>DECLARE @RowCount INT
  SET @RowCount = (SELECT Max(FogTopicsID) FROM FSB_FogTopics ) 
  
  DECLARE @I INT
  
  SET @I = 1
  
  WHILE (@I &lt;= @RowCount)
  BEGIN
      DECLARE @FogID INT, @StudentID INT, @TopicID INT, @ProcessStudentId INT 
  
      SELECT @FogID = FogID, @StudentID = StudentID,  @TopicID = TopicsID 
      FROM FSB_FogTopics 
      WHERE FogTopicsID = @I
  
      SELECT @ProcessStudentId = ProStudentId 
      FROM FSB_ProcessStudents 
      WHERE ProcessId = @FogID AND StudentId = @StudentID
  
      INSERT INTO FSB_ProcessTopics( [ProcessStudentId], [TopicId]) 
      VALUES (@ProcessStudentId, @TopicID)
  
      SET @I = @I + 1
  END
  </code></pre>
  
  <p>but I get an error </p>
  
  <blockquote>
    <p>Must Declare the Scalar Variable <code>@ProcessStudentId</code></p>
  </blockquote>