退出存储过程

时间:2010-04-28 10:23:15

标签: sql-server stored-procedures exit

我有一些循环和条件。如果条件匹配,那么我想停止或退出存储过程。怎么做?

while @@fetch_status=0
    begin
        if x=0
            'exit stored procedure
    end

3 个答案:

答案 0 :(得分:11)

如果您使用的是Microsoft Sql Server,则可以使用Return Statement

while @@fetch_status=0 begin if x=0 return; end

答案 1 :(得分:4)

通过@@fetch_status,它看起来像是你的光标循环内部,所以我会返回,因为你将跳过自己整理。

...
if x=0
  GOTO DONE
...
/* at the end of the sp */
DONE:
  CLOSE @your_cur
  DEALLOCATE @your_cur

答案 2 :(得分:1)

尝试使用return

while @@fetch_status=0
    begin
        if x=0
            return
    end