Skip to content

Commit 095147c

Browse files
authored
plpgsql NULL and RETURN STATEMENTS (#18623)
* plpgsql RETURN statements with no expression * plpgsql NULL statements
1 parent 6a419be commit 095147c

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/current/v24.1/create-function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ If you grant `EXECUTE` privilege as a default privilege at the database level, n
3131
Parameter | Description
3232
----------|------------
3333
`routine_create_name` | The name of the function.
34-
`routine_param` | A comma-separated list of function parameters.
34+
`routine_param` | A comma-separated list of function parameters, specifying the mode, name, and type.
3535
`routine_return_type` | The type returned by the function.
3636
`routine_body_str` | The body of the function. For allowed contents, see [User-Defined Functions]({% link {{ page.version.version }}/user-defined-functions.md %}#overview).
3737

src/current/v24.1/create-procedure.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ If you grant `EXECUTE` privilege as a default privilege at the database level, n
2929
| Parameter | Description |
3030
|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|
3131
| `routine_create_name` | The name of the procedure. |
32-
| `routine_param` | A comma-separated list of procedure parameters. |
33-
| `routine_body_str` | The body of the procedure. For allowed contents, see [Stored Procedures]({% link {{ page.version.version }}/stored-procedures.md %}#structure). |
32+
| `routine_param` | A comma-separated list of procedure parameters, specifying the mode, name, and type. |
33+
| `routine_body_str` | The body of the procedure. For allowed contents, see [Stored Procedures]({% link {{ page.version.version }}/stored-procedures.md %}#structure). |
3434

3535
## Examples
3636

src/current/v24.1/plpgsql.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ At the highest level, a PL/pgSQL block looks like the following:
4040
END
4141
~~~
4242

43-
PL/pgSQL blocks can be nested. An optional label can be placed above each block. Block labels can be targeted by [`EXIT` statements](#exit-and-continue-statements).
43+
PL/pgSQL blocks can be nested. An optional label can be placed above each block. Block labels can be targeted by [`EXIT` statements](#exit).
4444

4545
~~~ sql
4646
[ <<outer_block>> ]
@@ -217,7 +217,6 @@ IF condition THEN
217217

218218
`IF ... THEN ... ELSIF` executes statements if a boolean condition is true. If the condition is false, each `ELSIF` condition is evaluated until one is true. The corresponding `ELSIF` statements are executed. If no `ELSIF` conditions are true, no statements are executed unless an `ELSE` clause is included, in which case the `ELSE` statements are executed.
219219

220-
{% include_cached copy-clipboard.html %}
221220
~~~ sql
222221
IF condition THEN
223222
statements;
@@ -230,6 +229,14 @@ IF condition THEN
230229
END IF;
231230
~~~
232231

232+
`IF`, `ELSE`, and `ELSIF` conditions are not required to execute statements. You can exclude any statements or add a placeholder `NULL` statement.
233+
234+
~~~ sql
235+
IF condition THEN
236+
NULL;
237+
END IF;
238+
~~~
239+
233240
For usage examples of conditional statements, see [Examples](#examples).
234241

235242
### Write loops
@@ -254,7 +261,9 @@ WHILE condition LOOP
254261

255262
For an example, see [Create a stored procedure that uses a `WHILE` loop]({% link {{ page.version.version }}/create-procedure.md %}#create-a-stored-procedure-that-uses-a-while-loop).
256263

257-
### `EXIT` and `CONTINUE` statements
264+
### Control execution flow
265+
266+
#### `EXIT`
258267

259268
Add an `EXIT` statement to end a [loop](#write-loops). An `EXIT` statement can be combined with an optional `WHEN` boolean condition.
260269

@@ -299,6 +308,18 @@ CREATE PROCEDURE p() AS $$
299308
$$ LANGUAGE PLpgSQL;
300309
~~~
301310

311+
#### `RETURN`
312+
313+
Add a `RETURN` statement to a routine with an `OUT` parameter or `VOID` return type to exit the routine immediately.
314+
315+
~~~ sql
316+
BEGIN
317+
...
318+
RETURN;
319+
~~~
320+
321+
#### `CONTINUE`
322+
302323
Add a `CONTINUE` statement to end the current iteration of a [loop](#write-loops), skipping any statements below `CONTINUE` and beginning the next iteration of the loop.
303324

304325
A `CONTINUE` statement can be combined with an optional `WHEN` boolean condition. In the following example, if a `WHEN` condition is defined and met, then `CONTINUE` causes the loop to skip the second group of statements and begin again.
@@ -459,6 +480,14 @@ BEGIN
459480
END
460481
~~~
461482

483+
`WHEN` conditions are not required to execute statements. You can exclude any statements or add a placeholder `NULL` statement.
484+
485+
~~~ sql
486+
EXCEPTION
487+
WHEN error THEN
488+
NULL;
489+
~~~
490+
462491
### Control transactions
463492

464493
Use a `COMMIT` or `ROLLBACK` statement within a PL/pgSQL [stored procedure]({% link {{ page.version.version }}/stored-procedures.md %}) to finish the current transaction and automatically start a new one.

0 commit comments

Comments
 (0)