不要使用存储过程

时间:2017-04-16 14:21:53

标签: sql sql-server

我在存储过程中有以下循环:

WHILE (@progress <= @total) BEGIN
    IF (EXISTS(SELECT * FROM test WHERE to_cancel = 1 AND id = @Id)) BEGIN
        UPDATE test SET status = 2 WHERE id = @Id;
        WAITFOR DELAY '00:00:05';           
        BREAK;
    END

    WAITFOR DELAY '00:00:01';

    BEGIN TRY
        IF (@progress >= 10) BEGIN
            ;THROW 50001, 'Error raised', 1
        END

        SET @progress = @progress + 1;

        UPDATE test
        SET progress = @progress
        WHERE id = @Id;     
    END TRY
    BEGIN CATCH
        UPDATE test SET status = -1 WHERE id = @Id;
        WAITFOR DELAY '00:00:05';           
        BREAK;      
    END CATCH
END;

抛出错误并且循环中断。但是,UPDATE中的CATCH未被执行。

有原因吗?

0 个答案:

没有答案