Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BREAK statement in PL/pgSQL

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

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

As per my understanding it should only break the inner for loop?

like image 456
user1844840 Avatar asked Mar 02 '13 10:03

user1844840


People also ask

How do you exit a loop in PostgreSQL?

In PostgreSQL, The EXIT statement is used to terminate all types of loops like unconditional loops, a while loop, or a for loop or terminate a block of code specified by the begin.. end keywords.

What is Nullif in PostgreSQL?

NULLIF in postgreSQL is an in-built conditional function that takes two arguments and returns null if two arguments are equal. Otherwise, it returns the first argument. The function returns NULL if either of the arguments is NULL .

What is return next in PostgreSQL?

RETURN NEXT can be used with both scalar and composite data types; with a composite result type, an entire “table” of results will be returned. RETURN QUERY appends the results of executing a query to the function's result set.

Can we use PL SQL in PostgreSQL?

PL/pgSQL is easy to learn and simple to use. PL/pgSQL comes with PostgreSQL by default. The user-defined functions and stored procedures developed in PL/pgSQL can be used like any built-in functions and stored procedures. PL/pgSQL inherits all user-defined types, functions, and operators.


1 Answers

There is no BREAK in PL/pgSQL.

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 labeled loop. Else, it concerns the inner loop.
RETURN exits from the function (so not applicable in a DO statement).

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

  • Loop in function does not work as expected
like image 53
Erwin Brandstetter Avatar answered Oct 08 '22 22:10

Erwin Brandstetter