【问题标题】:Exit from a stored procedure退出存储过程
【发布时间】:2010-04-28 10:23:14
【问题描述】:

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

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

【问题讨论】:

  • 您使用的是哪种类型和版本的Sql? SQL Server、Oracle、MySql? @@fetch_status 确实建议使用 sql server。
  • 接受回答它对你有用

标签: sql-server stored-procedures exit


【解决方案1】:

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

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

【讨论】:

  • 谢谢回答..我能问你更多问题吗.呵呵..因为我是 sql 的新手...thx b4...如果我在@@fetch_status=0 时进行 hv 2 循环怎么样begin set y=y+1 set x=0 while @@fetch_status=0 begin x=y+1 if y = 5 '退出第二个do while并返回第一个do while --> y=y+1 end end
  • 是的,在这种情况下,您必须使用以下答案中建议的 goto 语句
【解决方案2】:

@@fetch_status 看起来你在一个游标循环中,所以我不会在那个时候返回,因为你自己会跳过整理。

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

【讨论】:

    【解决方案3】:

    尝试使用返回

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 2010-12-24
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 2021-06-03
      • 2021-12-27
      相关资源
      最近更新 更多