Infinite loop with SQL Server

时间:2017-04-06 17:07:38

标签: sql sql-server

I have the following script

SELECT
    RowNum = ROW_NUMBER() OVER (ORDER BY [RefAbacus]), * 
INTO 
    #CI
FROM 
    [CI]
WHERE 
    [RefAbacus] = @RefAbacus
ORDER BY 
    date, TypeOperation

SELECT
    RowNum = ROW_NUMBER() OVER (ORDER BY [RefAbacus]), * 
INTO 
    #DBRapproEchRemb
FROM 
    [DBTmpRapproEchRemb]
WHERE 
    [RefAbacus] = @RefAbacus
ORDER BY 
    date, TypeOperation

CREATE UNIQUE CLUSTERED INDEX IX_1 ON #CI ([RefAbacus], RowNum)
CREATE UNIQUE CLUSTERED INDEX IX_2 ON #DBRapproEchRemb ([RefAbacus], RowNum)

--declaration des variables
DECLARE @MaxRownum int
SET @MaxRownum = (SELECT MAX(RowNum) FROM #CI) 

DECLARE @MinRownum int
SET @MinRownum = (SELECT MIN(RowNum) FROM #CI)

DECLARE @FR int;
DECLARE @LR int;
DECLARE @inte int;
DECLARE @R int;
DECLARE @remboursement AS float
DECLARE @LastInterestDate AS nvarchar(50);
DECLARE @i int;
DECLARE @TolerancePart int;

SET @FR = @MinRownum
SET @LR = @MaxRownum
SET @TolerancePart = 5000;

SET @R = (SELECT MIN(RowNum) FROM #CI)
SET @inte = (SELECT MIN(RowNum) FROM #CI)

WHILE @R <= @MaxRownum
BEGIN
      WAY:
        --ELSE
        --BEGIN
        SET @LastInterestDate = ''
        SET @inte = (SELECT
          MIN(RowNum)
        FROM #CI)
        WHILE @inte <= @MaxRownum
        BEGIN -- run your operation here
          IF ((SELECT
              Date
            FROM #CI
            WHERE RowNum = @inte)
            <= CONVERT(date, GETDATE()))
          BEGIN

            IF ((SELECT
                EchInteret
              FROM #CI
              WHERE RowNum = @inte)
              IS NOT NULL and (SELECT
                    [Remboursement]
                  FROM #CI
                  WHERE RowNum = @R)IS NOT NULL)
            BEGIN

              IF ((SELECT
                  EchInteret
                FROM #CI
                WHERE RowNum = @inte)
                <> 0)
              BEGIN


                IF ((SELECT
                    [Remboursement]
                  FROM #CI
                  WHERE RowNum = @R)
                  > (SELECT
                    EchInteret
                  FROM #CI
                  WHERE RowNum = @inte)
                  )
                BEGIN ---------------------------------------
                  PRINT 'update 1'
                  UPDATE #CI
                  SET [Remboursement] = R.[Remboursement] - INTE.EchInteret
                  FROM (SELECT
                    *
                  FROM #CI
                  WHERE RowNum = @inte) INTE
                  INNER JOIN (SELECT
                    *
                  FROM #CI
                  WHERE RowNum = @R) R
                    ON R.RefAbacus = INTE.RefAbacus
                  WHERE #CI.RowNum = @R
                  AND #CI.[Remboursement] IS NOT NULL;





                  UPDATE #DBRapproEchRemb
                  SET [PartEnInterêt] = EchInteret
                  WHERE RowNum = @inte
                  AND #DBRapproEchRemb.EchInteret IS NOT NULL;

                  UPDATE #CI
                  SET EchInteret = 0
                  FROM #CI

                  WHERE #CI.RowNum = @inte


                END
                ELSE -------------------------
                 IF ((SELECT
                    [Remboursement]
                  FROM #CI
                  WHERE RowNum = @R)
                  <= (SELECT
                    EchInteret
                  FROM #CI
                  WHERE RowNum = @inte)
                  )
                BEGIN
                  PRINT 'update 2'
                  UPDATE #DBRapproEchRemb
                  SET [PartEnInterêt] = INTE.[PartEnInterêt] + R.Remboursement
                  FROM (SELECT
                    *
                  FROM #CI
                  WHERE RowNum = @R) R
                  INNER JOIN (SELECT
                    *
                  FROM #DBRapproEchRemb
                  WHERE RowNum = @inte) INTE
                    ON R.RefAbacus = INTE.RefAbacus
                  WHERE #DBRapproEchRemb.RowNum = @inte;


                  SELECT
                    INTE.[PartEnInterêt],
                    R.Remboursement
                  FROM (SELECT
                    *
                  FROM #CI
                  WHERE RowNum = @R) R
                  LEFT JOIN (SELECT
                    *
                  FROM #DBRapproEchRemb
                  WHERE RowNum = @inte) INTE
                    ON R.RefAbacus = INTE.RefAbacus
                  WHERE INTE.RowNum = @inte;

                  UPDATE INTE
                  SET EchInteret = INTE.EchInteret - R.Remboursement
                  FROM (SELECT
                    *
                  FROM #CI
                  WHERE RowNum = @R
                  AND Remboursement IS NOT NULL) R
                  INNER JOIN #CI
                  INTE
                    ON R.RefAbacus = INTE.RefAbacus
                  WHERE INTE.RowNum = @inte
                  AND INTE.EchInteret IS NOT NULL;


                  UPDATE #CI
                  SET Remboursement = 0
                  WHERE #CI.RowNum = @R
                  AND #CI.Remboursement IS NOT NULL; ;

                --------------------------------------------

                END;
                BREAK;
              END;
            END

            IF ((SELECT
                [Remboursement]
              FROM #CI
              WHERE RowNum = @R)
              IS NOT NULL and (SELECT
                      EchPrincial
                    FROM #CI
                    WHERE RowNum = @inte
                   )

                    IS NOT NULL)
            BEGIN
              IF (SELECT
                  [Remboursement]
                FROM #CI
                WHERE RowNum = @R
           )
                <> 0
              BEGIN
                IF ((SELECT
                    EchPrincial
                  FROM #CI
                  WHERE RowNum = @inte
                )
                  <> 0)
                BEGIN

                  IF ((SELECT
                      [Remboursement]
                    FROM #CI
                    WHERE RowNum = @R
                    )
                    > (SELECT
                      EchPrincial
                    FROM #CI
                    WHERE RowNum = @inte
                    )
                    )
                  BEGIN
                    PRINT 'update 3'
                    UPDATE #CI
                    SET [Remboursement] = R.[Remboursement] - INTE.EchPrincial
                    FROM (SELECT
                      *
                    FROM #CI
                    WHERE RowNum = @inte) INTE
                    INNER JOIN (SELECT
                      *
                    FROM #CI
                    WHERE RowNum = @R) R
                      ON R.RefAbacus = INTE.RefAbacus
                    WHERE #CI.RowNum = @R;

                    UPDATE #DBRapproEchRemb
                    SET PartEnPrincipal = EchPrincial
                    WHERE RowNum = @inte;


                    UPDATE #CI
                    SET EchPrincial = 0
                    WHERE #CI.RowNum = @inte;

                  END

                  -----------
                  ELSE
                    IF ((SELECT
                      [Remboursement]
                    FROM #CI
                    WHERE RowNum = @R
                    )
                    <=(SELECT
                      EchPrincial
                    FROM #CI
                    WHERE RowNum = @inte
                   )
                    )
                  BEGIN
                    PRINT 'update 4'
                    UPDATE INTE
                    SET [PartEnInterêt] = inte.[PartEnInterêt] + R.Remboursement
                    FROM #DBRapproEchRemb
                    INTE
                    INNER JOIN (SELECT
                      *
                    FROM #CI
                    WHERE RowNum = @R) R
                      ON R.RefAbacus = INTE.RefAbacus
                    WHERE INTE.RowNum = @inte;


                    UPDATE #CI
                    SET EchPrincial = inte.EchPrincial - R.Remboursement
                    FROM (SELECT
                      *
                    FROM #CI
                    WHERE RowNum = @inte) INTE
                    INNER JOIN (SELECT
                      *
                    FROM #CI
                    WHERE RowNum = @R) R
                      ON R.RefAbacus = INTE.RefAbacus
                    WHERE #CI.RowNum = @inte;


                    UPDATE #CI
                    SET Remboursement = 0
                    WHERE #CI.RowNum = @R;

                  END;
                  BREAK;
                END
              END
            END
          END
          SET @inte = @inte + 1;
        END ----end while 
        IF ((SELECT
            [Remboursement]
          FROM #CI
          WHERE RowNum = @R)
          <> 0 and (SELECT
            [Remboursement]
          FROM #CI
          WHERE RowNum = @R)
          is not null)
          GOTO WAY;

        SELECT
          *
        FROM #CI

        SELECT
          *
        FROM #DBRapproEchRemb
        SET @R = @R + 1;




      -------------------------------


      --END

      END

There is an infinite loop . How to correct it ?

0 个答案:

没有答案