> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# DO

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

The `DO` <InternalLink path="sql-statements">statement</InternalLink> defines a code block that executes <InternalLink path="plpgsql">PL/pgSQL</InternalLink> syntax.

## Required privileges

* To define a `DO` block with a <InternalLink path="create-type">user-defined type</InternalLink>, a user must have `USAGE` privilege on the user-defined type.

## Synopsis

<img src="https://mintcdn.com/cockroachlabs/uBcLAizjWFXF4pfd/images/sql-diagrams/v25.1/do.svg?fit=max&auto=format&n=uBcLAizjWFXF4pfd&q=85&s=2ea3088e406d4513c0b52290d1222335" alt="do syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="397" height="97" data-path="images/sql-diagrams/v25.1/do.svg" />

## 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 <InternalLink path="plpgsql#declare-a-variable">PL/pgSQL `DECLARE` syntax</InternalLink> to declare variables to use in the code block.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
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 <InternalLink path="plpgsql#write-loops">PL/pgSQL `WHILE` syntax</InternalLink> to loop through several statements.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
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 <InternalLink path="common-table-expressions">common table expression</InternalLink> in the body of the code block.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
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

* <InternalLink path="plpgsql">PL/pgSQL</InternalLink>
* <InternalLink path="stored-procedures">Stored Procedures</InternalLink>
* <InternalLink path="user-defined-functions">User-Defined Functions</InternalLink>
