# Labels

## Syntax

```sql
label: <construct>
[label]
```

Labels are MariaDB [identifiers](https://mariadb.com/docs/server/reference/sql-structure/sql-language-structure/identifier-names) which can be used to identify a [BEGIN ... END](https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/begin-end) construct or a loop. They have a maximum length of 16 characters and can be quoted with backticks (i.e.., \`\`\`).

Labels have a start part and an end part. The start part must precede the portion of code it refers to, must be followed by a colon (`:`) and can be on the same or different line. The end part is optional and adds nothing, but can make the code more readable. If used, the end part must precede the construct's delimiter (`;`). Constructs identified by a label can be nested. Each construct can be identified by only one label.

Labels need not be unique in the stored program they belong to. However, a label for an inner loop cannot be identical to a label for an outer loop. In this case, the following error would be produced:

```sql
ERROR 1309 (42000): Redefining label <label_name>
```

[LEAVE](https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/leave) and [ITERATE](https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/iterate) statements can be used to exit or repeat a portion of code identified by a label. They must be in the same [Stored Routine](https://mariadb.com/docs/server/server-usage/stored-routines), [Trigger](https://mariadb.com/docs/server/server-usage/triggers-events/triggers) or [Event](https://mariadb.com/docs/server/server-usage/triggers-events/event-scheduler/events) which contains the target label.

Below is an example using a simple label that is used to exit a [LOOP](https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/loop):

```sql
CREATE PROCEDURE `test_sp`()
BEGIN
   `my_label`:
   LOOP
      SELECT 'looping';
      LEAVE `my_label`;
   END LOOP;
   SELECT 'out of loop';
END;
```

The following label is used to exit a procedure, and has an end part:

```sql
CREATE PROCEDURE `test_sp`()
`my_label`:
BEGIN
   IF @var = 1 THEN
      LEAVE `my_label`;
   END IF;
   DO something();
END `my_label`;
```

<sub>*This page is licensed: CC BY-SA / Gnu FDL*</sub>

{% @marketo/form formId="4316" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/labels.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
