-
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
DO blocks #19356
Changes from 1 commit
4e39941
1c0e466
dfe9c56
7f696c5
81c2dfd
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- | ||
title: DO | ||
summary: The DO statement executes a SQL or 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 SQL or [PL/pgSQL]({% link {{ page.version.version }}/plpgsql.md %}) syntax. | ||
|
||
## Required privileges | ||
|
||
- To execute a `DO` block, a user must have at least the [`USAGE` privilege]({% link {{ page.version.version }}/security-reference/authorization.md %}#supported-privileges) on the schema where the `DO` block is being executed. | ||
- 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 | ||
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. Note there's also an optional 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. 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 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.
Yeah, that sounds good. |
||
|
||
| 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 | ||
~~~ |
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.
Can you explain this one? I don't think it's true in either CRDB or Postgres.
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 sorry, that was kind of a guess. I ported it over from the CREATE PROCEDURE and CREATE FUNCTION prereqs. What exactly are the privileges we need on DO blocks? The PG doc says
The user must have USAGE privilege for the procedural language, or must be a superuser if the language is untrusted. This is the same privilege requirement as for creating a function in the language.
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.
We don't currently do language privileges AFAIK, so I think we should just leave that out for now.