-
Notifications
You must be signed in to change notification settings - Fork 469
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
DO blocks #19356
Merged
+114
−3
Merged
DO blocks #19356
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4e39941
DO blocks
taroface 1c0e466
address reviewer feedback
taroface dfe9c56
expand front-matter
taroface 7f696c5
address reviewer comment; make correction to SQL statements table
taroface 81c2dfd
Merge branch 'main' into do-statement
taroface File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
title: DO | ||
summary: The DO statement executes a PL/pgSQL code block. | ||
toc: true | ||
keywords: | ||
docs_area: reference.sql | ||
--- | ||
|
||
The `DO` [statement]({% link {{ page.version.version }}/sql-statements.md %}) defines a code block that executes [PL/pgSQL]({% link {{ page.version.version }}/plpgsql.md %}) syntax. | ||
|
||
## Required privileges | ||
|
||
- To define a `DO` block with a [user-defined type]({% link {{ page.version.version }}/create-type.md %}), a user must have `USAGE` privilege on the user-defined type. | ||
|
||
## Synopsis | ||
|
||
<div> | ||
{% remote_include https://raw.githubusercontent.com/cockroachdb/generated-diagrams/{{ page.release_info.crdb_branch_name }}/grammar_svg/do.html %} | ||
</div> | ||
|
||
## Parameters | ||
|
||
| Parameter | Description | | ||
|--------------------|-----------------------------| | ||
| `routine_body_str` | The body of the code block. | | ||
|
||
## Examples | ||
|
||
### Declare a variable in a `DO` block | ||
|
||
The following example uses the [PL/pgSQL `DECLARE` syntax]({% link {{ page.version.version }}/plpgsql.md %}#declare-a-variable) to declare variables to use in the code block. | ||
|
||
{% include_cached copy-clipboard.html %} | ||
~~~ sql | ||
DO $$ | ||
DECLARE | ||
x INT := 10; | ||
y INT := 5; | ||
result INT; | ||
BEGIN | ||
result := x + y; | ||
RAISE NOTICE 'The sum of % and % is %', x, y, result; | ||
END $$; | ||
~~~ | ||
|
||
~~~ | ||
NOTICE: The sum of 10 and 5 is 15 | ||
DO | ||
~~~ | ||
|
||
### Use a loop in a `DO` block | ||
|
||
The following example uses the [PL/pgSQL `WHILE` syntax]({% link {{ page.version.version }}/plpgsql.md %}#write-loops) to loop through several statements. | ||
|
||
{% include_cached copy-clipboard.html %} | ||
~~~ sql | ||
DO $$ | ||
DECLARE | ||
counter INT := 1; | ||
BEGIN | ||
WHILE counter <= 5 LOOP | ||
RAISE NOTICE 'Counter: %', counter; | ||
counter := counter + 1; | ||
END LOOP; | ||
END $$; | ||
~~~ | ||
|
||
~~~ | ||
NOTICE: Counter: 1 | ||
NOTICE: Counter: 2 | ||
NOTICE: Counter: 3 | ||
NOTICE: Counter: 4 | ||
NOTICE: Counter: 5 | ||
DO | ||
~~~ | ||
|
||
### Use a common table expression in a `DO` block | ||
|
||
The following example uses a [common table expression]({% link {{ page.version.version }}/common-table-expressions.md %}) in the body of the code block. | ||
|
||
{% include_cached copy-clipboard.html %} | ||
~~~ sql | ||
DO $$ | ||
DECLARE | ||
sum_result INT; | ||
BEGIN | ||
WITH numbers AS ( | ||
SELECT generate_series(1, 5) AS num | ||
) | ||
SELECT sum(num) INTO sum_result | ||
FROM numbers; | ||
|
||
RAISE NOTICE 'Sum of numbers 1-5: %', sum_result; | ||
END $$; | ||
~~~ | ||
|
||
~~~ | ||
NOTICE: Sum of numbers 1-5: 15 | ||
DO | ||
~~~ | ||
|
||
## See also | ||
|
||
- [PL/pgSQL]({% link {{ page.version.version }}/plpgsql.md %}) | ||
- [Stored Procedures]({% link {{ page.version.version }}/stored-procedures.md %}) | ||
- [User-Defined Functions]({% link {{ page.version.version }}/user-defined-functions.md %}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Note there's also an optional
LANGUAGE foo
. SQL is disallowed and we currently only support PL/pgSQL out of the options Postgres allows.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.
Yep, this will be in the diagram (will open that PR soon). But I didn't know about SQL being disallowed. So the diagram should only show an optional
LANGUAGE PLPGSQL
?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.
Yeah, that sounds good.