title: Foreach row, update another table row if exist else insert a new record
body: <p>I have a query which returns a number of rows. </p>
  
  <p>In following code, one <code>EnrollmentId</code> can have multiple <code>StudentId</code>s.</p>
  
  <pre><code>SELECT 
      [StudentId]
      , [ennrollmentId]
      , [Name]
      , [StartDate]
      , [Enddate]
  FROM 
      [Enrollements]
  WHERE 
      [ennrollmentId] = @ennrollmentId;
  </code></pre>
  
  <p>Now I want to insert <code>StudentId</code>, <code>EnrollemntId</code>, <code>StartDate</code> and <code>EndDate</code> into a new table with these rules, if no row exists for <code>StudentId</code> and <code>EnrollmentId</code>; if such a row already exists, then update the <code>StartDate</code> and <code>EndDate</code>.</p>
  
  <p>I wrote following query:</p>
  
  <pre><code>INSERT INTO [AnotherTable]
      SELECT *
      FROM [Enrollements]
      WHERE [enrollmentId] = @enrollmentId;  
  
  -- I have enrollmentId as stored procedure parameter 
  </code></pre>
  
  <p>But this always inserts a new record, it does not update.</p>