title: Update column of Table B based on column of Table A
body: <p>I have a huge stored  procedure and I am trying to optimize it . </p>
  
  <p>I have a temporary table <strong>A</strong> , with column <strong>Id</strong>.</p>
  
  <p>I have a main table B with columns <strong>Id</strong> &amp; a boolean field <strong>Test</strong></p>
  
  <p>For all Id's in Table A , I need to make the Test = 1 in Table B.</p>
  
  <p>example :
  <strong>A</strong></p>
  
  <pre><code>Id
  --
  1
  2
  3
  </code></pre>
  
  <p>I need to get table B as follows.
  I already have the Id field in Table B , I only need to update the 
  Test Column
      <strong>B</strong></p>
  
  <pre><code>Id Test
  -- ----
  1   1
  2   1
  3   1
  4
  5
  6
  </code></pre>
  
  <p>I am currently looping through Table <strong>A</strong> using a while loop and for each Id
  I am updating the Test column in Table <strong>B</strong> .</p>
  
  <pre><code>WHILE (1 = 1) 
  BEGIN  
  
    -- Exit loop if no more Transactions
    IF @@ROWCOUNT = 0 BREAK;
  
    Update [B]
    set Test = 1 
    where Id = (SELECT TOP 1 Id
    FROM #B
    WHERE Id &gt; @Id1 
    ORDER BY Id)    
  
  END
  </code></pre>
  
  <p>PS : @Id1 is the input parameter of the stored procedure.</p>
  
  <p>I cannot think of any other efficient way of doing it, but 
  my query is taking a lot of time to run..
  Please let me know if there is a better way of doing the same thing.</p>