Skip to content
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
merged 5 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/current/_includes/v25.1/sidebar-data/sql.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@
"/${VERSION}/delete.html"
]
},
{
"title": "<code>DO</code>",
"urls": [
"/${VERSION}/do.html"
]
},
{
"title": "<code>DROP DATABASE</code>",
"urls": [
Expand Down
106 changes: 106 additions & 0 deletions src/current/v25.1/do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
title: DO
summary: The DO SQL 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

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.

Copy link
Contributor Author

@taroface taroface Feb 6, 2025

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?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the diagram should only show an optional LANGUAGE PLPGSQL?

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
~~~

## 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 %})
1 change: 1 addition & 0 deletions src/current/v25.1/sql-statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Statement | Usage
[`CREATE TABLE AS`]({% link {{ page.version.version }}/create-table-as.md %}) | Create a new table in a database using the results from a [selection query]({% link {{ page.version.version }}/selection-queries.md %}).
[`COPY FROM`]({% link {{ page.version.version }}/copy-from.md %}) | Copy data from a third-party client to a CockroachDB cluster.<br>For compatibility with PostgreSQL drivers and ORMs, CockroachDB supports `COPY FROM` statements issued only from third-party clients; you cannot issue `COPY FROM` statements from the [`cockroach` SQL shell]({% link {{ page.version.version }}/cockroach-sql.md %}). To import data from files, use an [`IMPORT INTO`]({% link {{ page.version.version }}/import-into.md %}) statement instead.
[`DELETE`]({% link {{ page.version.version }}/delete.md %}) | Delete specific rows from a table.
[`DO`]({% link {{ page.version.version }}/do.md %}) | Execute a SQL or [PL/pgSQL]({% link {{ page.version.version }}/plpgsql.md %}) code block.
[`EXPORT`]({% link {{ page.version.version }}/export.md %}) | Export an entire table's data, or the results of a `SELECT` statement, to CSV files.
[`IMPORT INTO`]({% link {{ page.version.version }}/import-into.md %}) | Bulk-insert CSV data into an existing table.
[`INSERT`]({% link {{ page.version.version }}/insert.md %}) | Insert rows into a table.
Expand Down
Loading