Skip to content

Commit 59ffbc0

Browse files
document RETURN NEXT and RETURN QUERY (#19589)
* document RETURN NEXT and RETURN QUERY --------- Co-authored-by: Florence Morris <[email protected]>
1 parent c4b6e82 commit 59ffbc0

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/current/_includes/v25.2/known-limitations/plpgsql-limitations.md

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
- PL/pgSQL arguments cannot be referenced with ordinals (e.g., `$1`, `$2`). [#114701](https://github.com/cockroachdb/cockroach/issues/114701)
66
- The following statements are not supported:
77
- `FOR` cursor loops, `FOR` query loops, and `FOREACH` loops. [#105246](https://github.com/cockroachdb/cockroach/issues/105246)
8-
- `RETURN NEXT` and `RETURN QUERY`. [#117744](https://github.com/cockroachdb/cockroach/issues/117744)
98
- `PERFORM`, `EXECUTE`, `GET DIAGNOSTICS`, and `CASE`. [#117744](https://github.com/cockroachdb/cockroach/issues/117744)
109
- PL/pgSQL exception blocks cannot catch [transaction retry errors]({% link {{ page.version.version }}/transaction-retry-error-reference.md %}). [#111446](https://github.com/cockroachdb/cockroach/issues/111446)
1110
- `RAISE` statements cannot be annotated with names of schema objects related to the error (i.e., using `COLUMN`, `CONSTRAINT`, `DATATYPE`, `TABLE`, or `SCHEMA`). [#106237](https://github.com/cockroachdb/cockroach/issues/106237)

src/current/v25.2/plpgsql.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,63 @@ CREATE PROCEDURE p() AS $$
343343

344344
#### `RETURN`
345345

346-
Add a `RETURN` statement to a routine with an `OUT` parameter or `VOID` return type to exit the routine immediately.
346+
Add a `RETURN` statement to a routine with an `OUT` parameter, `RETURNS VOID` clause, or `RETURNS SETOF` clause to exit the routine immediately.
347347

348348
~~~ sql
349349
BEGIN
350350
...
351351
RETURN;
352352
~~~
353353

354+
Add a `RETURN` statement in a scalar-returning function to return the result of an expression.
355+
356+
The following example uses `RETURN` to return the square of the input argument.
357+
358+
{% include_cached copy-clipboard.html %}
359+
~~~ sql
360+
CREATE FUNCTION square(x INT) RETURNS INT AS $$
361+
BEGIN
362+
RETURN x * x;
363+
END;
364+
$$ LANGUAGE PLpgSQL;
365+
~~~
366+
367+
#### `RETURN NEXT` and `RETURN QUERY`
368+
369+
Add `RETURN NEXT` or `RETURN QUERY` statements to a [set-returning function]({% link {{ page.version.version }}/create-function.md %}#create-a-function-that-returns-a-set-of-results) to append rows to the result set. You can combine `RETURN NEXT` and `RETURN QUERY` statements in a single function to build the result set.
370+
371+
Use `RETURN NEXT` within a set-returning function to append a row to the result set.
372+
373+
In the following example, `RETURN NEXT` returns a new row during each loop iteration.
374+
375+
{% include_cached copy-clipboard.html %}
376+
~~~ sql
377+
CREATE FUNCTION get_numbers() RETURNS SETOF INT AS $$
378+
DECLARE
379+
i INT := 1;
380+
BEGIN
381+
WHILE i <= 5 LOOP
382+
RETURN NEXT i;
383+
i := i + 1;
384+
END LOOP;
385+
END
386+
$$ LANGUAGE PLpgSQL;
387+
~~~
388+
389+
Use `RETURN QUERY` within a set-returning function to append the results of a SQL query to the result set.
390+
391+
In the following example, `RETURN QUERY` returns all qualifying rows from the `SELECT` query.
392+
393+
{% include_cached copy-clipboard.html %}
394+
~~~ sql
395+
CREATE FUNCTION get_even_numbers() RETURNS SETOF INT AS $$
396+
BEGIN
397+
RETURN QUERY
398+
SELECT i FROM generate_series(1, 10) AS i WHERE i % 2 = 0;
399+
END
400+
$$ LANGUAGE PLpgSQL;
401+
~~~
402+
354403
#### `CONTINUE`
355404

356405
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.

0 commit comments

Comments
 (0)