-
Notifications
You must be signed in to change notification settings - Fork 470
document RETURN NEXT and RETURN QUERY #19589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -343,14 +343,63 @@ CREATE PROCEDURE p() AS $$ | |||||
|
||||||
#### `RETURN` | ||||||
|
||||||
Add a `RETURN` statement to a routine with an `OUT` parameter or `VOID` return type to exit the routine immediately. | ||||||
Add a `RETURN` statement to a routine with an `OUT` parameter, `RETURNS VOID` clause, or `RETURNS SETOF` clause to exit the routine immediately. | ||||||
|
||||||
~~~ sql | ||||||
BEGIN | ||||||
... | ||||||
RETURN; | ||||||
~~~ | ||||||
|
||||||
Add a `RETURN` statement in a scalar-returning function to return the result of an expression. | ||||||
|
||||||
The following example uses `RETURN` to return the square of the input argument. | ||||||
|
||||||
{% include_cached copy-clipboard.html %} | ||||||
~~~ sql | ||||||
CREATE FUNCTION square(x INT) RETURNS INT AS $$ | ||||||
BEGIN | ||||||
RETURN x * x; | ||||||
END; | ||||||
$$ LANGUAGE PLpgSQL; | ||||||
~~~ | ||||||
|
||||||
#### `RETURN NEXT` and `RETURN QUERY` | ||||||
taroface marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
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. | ||||||
|
||||||
Use `RETURN NEXT` within a set-returning function to append a row to the result set. | ||||||
|
||||||
In the following example, `RETURN NEXT` returns a new row during each loop iteration. | ||||||
|
||||||
{% include_cached copy-clipboard.html %} | ||||||
~~~ sql | ||||||
CREATE FUNCTION get_numbers() RETURNS SETOF INT AS $$ | ||||||
DECLARE | ||||||
i INT := 1; | ||||||
BEGIN | ||||||
WHILE i <= 5 LOOP | ||||||
RETURN NEXT i; | ||||||
i := i + 1; | ||||||
END LOOP; | ||||||
END | ||||||
$$ LANGUAGE PLpgSQL; | ||||||
~~~ | ||||||
|
||||||
Use `RETURN QUERY` within a set-returning function to append the results of a SQL query to the result set. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clarification - can it be anything other than a
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it can, such as |
||||||
|
||||||
In the following example, `RETURN QUERY` returns all qualifying rows from the `SELECT` query. | ||||||
|
||||||
{% include_cached copy-clipboard.html %} | ||||||
~~~ sql | ||||||
CREATE FUNCTION get_even_numbers() RETURNS SETOF INT AS $$ | ||||||
BEGIN | ||||||
RETURN QUERY | ||||||
SELECT i FROM generate_series(1, 10) AS i WHERE i % 2 = 0; | ||||||
END | ||||||
$$ LANGUAGE PLpgSQL; | ||||||
~~~ | ||||||
|
||||||
#### `CONTINUE` | ||||||
|
||||||
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. | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going to keep the original for consistency with the other sections!