T-SQL游标中的无限循环

时间:2012-10-30 21:59:14

标签: tsql cursor

以下是T-SQL游标的代码。它在第一次迭代中工作正常,但之后陷入FETCH NEXT语句和IF NOT EXISTS语句之间的无限循环(基本上它会插入第一条记录,但之后光标将不会移动到下一条记录所以IF NOT EXISTS永远是假的。这是我第一次使用光标,所以希望有人可以解释发生了什么/如何让这个东西工作!

    DECLARE prod_cursor CURSOR FOR
    SELECT ProductCode 
    FROM CourseToProduct 
    WHERE CourseCode = @courseCode and (TerminationDate >= @expDate OR TerminationDate IS NULL)

    OPEN prod_cursor

    FETCH NEXT FROM prod_cursor
    INTO @productCode

    WHILE @@FETCH_STATUS = 0
    BEGIN
        IF NOT EXISTS
        (
        SELECT sNumber
        FROM AgentProductTraining
        WHERE @sNumber = sNumber and 
              @courseCode = CourseCode and 
              @productCode = ProductCode and 
              @dateTaken = DateTaken
        )
        BEGIN
            IF @sNumber IS NOT NULL
            BEGIN
                INSERT INTO AgentProductTraining
                            (
                             sNumber,
                             CourseCode,
                             ProductCode,
                             DateTaken,
                             DateExpired,
                             LastChangeOperator,
                             LastChangeDate
                            ) 
                VALUES      (
                             @sNumber,
                             @courseCode,
                             @productCode,
                             @dateTaken,
                             COALESCE(@expDate, 'NULL'),
                             @lastChangeOperator,
                             @lastChangeDate
                           )    
            END
        END
    END
    CLOSE prod_cursor;
    DEALLOCATE prod_cursor;

1 个答案:

答案 0 :(得分:6)

您必须获取while..end中的下一行,否则它将永远不会移动到下一条记录。像这样:

DECLARE prod_cursor CURSOR FOR
SELECT ProductCode 
FROM CourseToProduct 
WHERE CourseCode = @courseCode and (TerminationDate >= @expDate OR TerminationDate IS NULL)

OPEN prod_cursor

FETCH NEXT FROM prod_cursor
INTO @productCode

WHILE @@FETCH_STATUS = 0
BEGIN
    IF NOT EXISTS
    (
    SELECT SymetraNumber
    FROM AgentProductTraining
    WHERE @symetraNumber = SymetraNumber and 
          @courseCode = CourseCode and 
          @productCode = ProductCode and 
          @dateTaken = DateTaken
    )
    BEGIN
        IF @symetraNumber IS NOT NULL
        BEGIN
            INSERT INTO AgentProductTraining
                        (
                         sNumber,
                         CourseCode,
                         ProductCode,
                         DateTaken,
                         DateExpired,
                         LastChangeOperator,
                         LastChangeDate
                        ) 
            VALUES      (
                         @sNumber,
                         @courseCode,
                         @productCode,
                         @dateTaken,
                         COALESCE(@expDate, 'NULL'),
                         @lastChangeOperator,
                         @lastChangeDate
                       )    
        END
    END

    FETCH NEXT FROM prod_cursor
    INTO @productCode
END
CLOSE prod_cursor;
DEALLOCATE prod_cursor;