'Migrating records < 2millions using stored procedure (Microsoft SQL Azure (RTM) - 12.0.2000.8)

I have following Stored procedure. TempSourceTable has almost 0.7million records or may be 10millions. If the DestinationTable already contains the row then update DestinationTable row otherwise insert a new record in to DestinationTable

After executing the StoredProcedure there are no errors. But DestinationTable only has 10001 records only. What is wrong with following stored procedure.

    CREATE PROCEDURE [dbo].[SP_Test] 
         @updatedCount int OUTPUT,
         @addedCount int OUTPUT
         AS
         BEGIN
           SET NOCOUNT ON;  
           DECLARE @updated int;
           DECLARE @added int;
           DECLARE @counter int;
           DECLARE @maxCount int;
           SET  @maxCount = (SELECT MAX(UniqueId) FROM #TempSourceTable);
           SET @counter = 1;
           SET @updated = 0;
           SET @added = 0;
           WHILE (@counter <= @maxCount)
             BEGIN
                Update DestinationTable
                    SET 
                        DestinationTable.Nome = #TempSourceTable.Nome,
                        DestinationTable.Cognome = #TempSourceTable.Cognome,
                        DestinationTable.CodiceFiscale = #TempSourceTable.CodiceFiscale,
                        DestinationTable.DataNascita = #TempSourceTable.DataNascita,
                        DestinationTable.Sesso = #TempSourceTable.Sesso,
                        DestinationTable.Email = #TempSourceTable.Email,
                        DestinationTable.Mobile = #TempSourceTable.Mobile,
                        DestinationTable.Telephone = #TempSourceTable.Telephone,
                        DestinationTable.Number = #TempSourceTable.Number,
                        DestinationTable.Street = #TempSourceTable.Street,
                        DestinationTable.City = #TempSourceTable.City,
                        DestinationTable.CAP = #TempSourceTable.CAP,
                        DestinationTable.Internal = #TempSourceTable.Internal,
                        DestinationTable.Province = #TempSourceTable.Province,
                        DestinationTable.USERID = #TempSourceTable.USERID
                    FROM  DestinationTable 
                    INNER JOIN #TempSourceTable  ON DestinationTable.CodiceFiscale = 
                       #TempSourceTable.CodiceFiscale
                        AND DestinationTable.CodiceCliente = #TempSourceTable.CodiceCliente
                    WHERE #TempSourceTable.UniqueId = @counter

            --SET @updated = @updated + 1;
            IF @@ROWCOUNT = 0
            BEGIN 
                --PRINT Concat('New insert with id: ', @counter)
                -- create a new patient
                INSERT INTO DestinationTable (Id, Nome, Cognome, CodiceFiscale, DataNascita, 
                            Sesso, Email, Mobile, Telephone, Number, Street, City, CAP, 
                Internal, Province, USERID,
                            DataInserimento, CodiceCliente)
                SELECT Id, Nome, Cognome, CodiceFiscale, DataNascita, 
                            Sesso, Email, Mobile, Telephone, Number, Street, City, CAP, 
                Internal, Province, USERID,
                            DataInserimento, CodiceCliente
                FROM #TempSourceTable
                WHERE UniqueId = @counter;

                SET @added = @added + 1;
            END
            ELSE
                BEGIN
                 -- patient was found so update it by csv file data
                --PRINT Concat('FoundExistingPatient with id: ', @counter);
                SET @updated = @updated + 1;
                END 

         SET @counter = @counter + 1;
    END
    SET @addedCount = @added;
    SET @updatedCount = @updated;
    END
    GO


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source