有没有办法在SQL SERVER中跳到WHILE循环的末尾?

时间:2015-02-13 20:34:22

标签: sql-server sql-server-2012

我的代码如下所示:

WHILE @@FETCH_STATUS = 0
    BEGIN

        IF (@Result = 'S') 

        -- A lot of code


        FETCH NEXT FROM CUR INTO @A

    END 

CLOSE CUR;
DEALLOCATE CUR;

我意识到我可以使用BEGIN END和ELSE并输入"很多代码"在那里,但有另一种方法,我可以做另一个FETCH并跳到WHILE的结尾?

1 个答案:

答案 0 :(得分:1)

BREAK and CONTINUE statements

你可以说:

WHILE @@FETCH_STATUS = 0
    BEGIN

        IF (@Result <> 'S') 
        BEGIN
            FETCH NEXT FROM CUR INTO @A;
            CONTINUE;
        END    

        -- A lot of code


        FETCH NEXT FROM CUR INTO @A

    END 

CLOSE CUR;
DEALLOCATE CUR;