title: CURSOR vs. UPDATE
body: <p>A company uses a SQL Server database to store information about its customers and its business transactions. A new area code has been introduced for your city. The area code 111 remains the same for telephone numbers with prefixes that are less than 500. The numbers with prefixes that are 500 and greater will be assigned an area code of 222. All telephone numbers in the Phone column in the Customers table are stored as char(12) strings of the following format, ‘999-999-9999’. i must make the appropriate changes to the Customers table </p>
  
  <p>as quickly as possible using the least administrative effort. Which one should I use ? </p>
  
  <p>a. </p>
  
  <pre><code>UPDATE Customers SET Phone = ‘222-‘ + SUBSTRING(Phone,5,8) 
  FROM Customers WHERE SUBSTRING(Phone,1,3) = ‘111’ 
  AND SUBSTRING(Phone,5,3) &gt;= 500 
  </code></pre>
  
  <p>b.   </p>
  
  <pre><code>DECLARE PhoneCursor CURSOR FOR 
     SELECT Phone FROM Customers 
     WHERE SUBSTRING(Phone,1,3) = 111 
       AND SUBSTRING(Phone,5,3) &gt;= 500 
  
  OPEN PhoneCursor 
  FETCH NEXT FROM PhoneCursor 
  
  WHILE @@FETCH_STATUS = 0 
  BEGIN 
      UPDATE Customers 
      SET Phone = ‘222’ + SUBSTRING(Phone,5,8) 
      WHERE CURRENT OF PhoneCursor 
  
      FETCH NEXT FROM PhoneCursor 
  END 
  
  CLOSE PhoneCursor 
  DEALLOCATE PhoneCursor 
  </code></pre>