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
BREAK statement in PL/pgSQL
提问by user1844840
How to have the break
statement 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 for
loop?
根据我的理解,它应该只打破内for
循环?
回答by Erwin Brandstetter
There is no in PL/pgSQL.BREAK
有没有在PL / pgSQL里。BREAK
EXIT
terminates the loop.CONTINUE
continues 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.RETURN
exits from the function (so not applicable in a DO
statement).
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