title: How Can I replace a cursor in sql server 2008
body: <p>In the last years I have put lot of effort in c# and left sql server a bit .
  My sql skills could be better.
  I know cursors are slow etc... I have put together a noddy example that I seem to encounter quite a bit at work.</p>
  
  <p>I need to migrate data from one flat table "Customer"</p>
  
  <p>into many tables </p>
  
  <p>"CustomerAddress" "CustomerPhone" etc..</p>
  
  <p>If you were assigned this task how would you do it without using cursors?</p>
  
  <p>Cursor to convert</p>
  
  <pre><code>            BEGIN TRANSACTION
  
  
              DECLARE @CustomerID int,
                      @Name nvarchar(50),
                      @Surname nvarchar(50),
                      @DateOfBirth datetime,
                      @Address nvarchar(200),
                      @City nvarchar(50),
                      @County nvarchar(50),
                      @Country nvarchar(50),
                      @HomePhone nvarchar(20)
  
  
              DECLARE OldCustomerCursor CURSOR FAST_FORWARD
              FOR
              SELECT CustomerID,Name,Surname,DateOfBirth,Address,City,County,Country,HomePhone
              FROM OldCustomer
  
              OPEN OldCustomerCursor
  
              FETCH NEXT FROM OldCustomerCursor INTO  @CustomerID,
                                                      @Name ,
                                                      @Surname ,
                                                      @DateOfBirth ,
                                                      @Address ,
                                                      @City ,
                                                      @County ,
                                                      @Country ,
                                                      @HomePhone
  
              WHILE @@FETCH_STATUS = 0
              BEGIN
  
  
                  INSERT [dbo].[Customer] ([CustomerID], [Name], [Surname], [DateOfBirth])
                  VALUES(@CustomerID,@Name,@Surname,@DateOfBirth)  
  
                  INSERT [CustomerAddress]([AddressID],[CustomerID],[Country],[Address],[City],[County])
                  VALUES(@Count,@CustomerID,@County,@Address,@City,@Country)
  
                  INSERT [dbo].[CustomerTelephone]([TelephoneID],[CustomerID],[Number])
                  VALUES(@Count,@CustomerID, @HomePhone)
  
                FETCH NEXT FROM OldCustomerCursor INTO @CustomerID,
                                                   @Name ,
                                                   @Surname ,
                                                   @DateOfBirth ,
                                                   @Address ,
                                                   @City ,
                                                   @County ,
                                                   @Country ,
                                                   @HomePhone
              END
  
              CLOSE OldCustomerCursor
              DEALLOCATE OldCustomerCursor
  
              SELECT  * FROM    Customer
              SELECT  * FROM    CustomerAddress
              SELECT  * FROM    CustomerTelephone
              ROLLBACK TRANSACTION
  </code></pre>
  
  <p>Thanks for any suggestions how to replace a cursor</p>