postgresql PL/pgSQL 中的 BREAK 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15173194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:03:57  来源:igfitidea点击:

BREAK statement in PL/pgSQL

postgresqlloopsplpgsqlbreak

提问by user1844840

How to have the breakstatement in PostgreSQL? I have the structure like this:

如何break在 PostgreSQL 中有语句?我有这样的结构:

for()
 {
 for()
 {
  if(somecondition)
  break;
 }
}

As per my understanding it should only break the inner forloop?

根据我的理解,它应该只打破内for循环?

回答by Erwin Brandstetter

There is no BREAKin PL/pgSQL.

有没有BREAKPL / pgSQL里

EXITterminates the loop.
CONTINUEcontinues at the next iteration of the loop.
You can attach a <<label>>to loops and add it as parameter to each of these commands. Then you terminate / continue the labeledloop. Else, it concerns the innerloop.
RETURNexits from the function (so not applicable in a DOstatement).

EXIT终止循环。
CONTINUE在循环的下一次迭代中继续。
您可以将 a 附加<<label>>到循环并将其作为参数添加到这些命令中的每一个。然后终止/继续标记循环。否则,它涉及循环。
RETURN从函数中退出(因此不适用于DO语句)。

All of this applies to procedural elements of PL/pgSQL, notSQL.
Code example using all three:

所有这些都适用于 PL/pgSQL 的过程元素,而不是SQL。
使用所有三个的代码示例:

回答by Fritz Lehmann-Grube

Note, that: Yes! you need the "WHEN", even if you end up (like me ;-)) with something like

请注意:是的!你需要“WHEN”,即使你最终(像我一样;-))

LOOP
  ...
  IF l_my_var = 'some condition' THEN
    -- this is ok, bla
    IF l_debug_level >= 2 THEN
      RAISE NOTICE 'debug 2: skipping a duplicate %, l_my_var;
    END IF;
    -- do something
    CONTINUE WHEN TRUE; -- https://stackoverflow.com/questions/15173194/break-statement-in-pl-pgsql
  ELSE
    -- do something
  END IF;
  ...

which looks somewhat twisted with both IF and WHEN.

IF 和 WHEN 看起来都有些扭曲。

Remark to the editor: One could make the link behind "CONTINUE" more precise: https://www.postgresql.org/docs/9.6/plpgsql-control-structures.html#AEN66440

对编辑的评论:可以使“继续”后面的链接更精确:https: //www.postgresql.org/docs/9.6/plpgsql-control-structures.html#AEN66440