> For the complete documentation index, see [llms.txt](https://mariadb.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/leave.md).

# LEAVE

## Syntax

```bnf
LEAVE label
```

This statement is used to exit the flow control construct that has the given [label](/docs/server/reference/sql-statements/programmatic-compound-statements/labels.md). The label must be in the same stored program, not in a caller procedure. `LEAVE` can be used within [BEGIN ... END](/docs/server/reference/sql-statements/programmatic-compound-statements/begin-end.md) or loop constructs ([LOOP](/docs/server/reference/sql-statements/programmatic-compound-statements/loop.md), [REPEAT](/docs/server/reference/sql-statements/programmatic-compound-statements/repeat-loop.md), [WHILE](/docs/server/reference/sql-statements/programmatic-compound-statements/while.md)). In [Stored Procedures](/docs/server/server-usage/stored-routines/stored-procedures.md), [Triggers](/docs/server/server-usage/triggers-events/triggers.md) and [Events](/docs/server/server-usage/triggers-events/event-scheduler/events.md), LEAVE can refer to the outmost `BEGIN ... END` construct; in that case, the program exits the procedure. In [Stored Functions](/docs/server/server-usage/stored-routines/stored-functions.md), [RETURN](/docs/server/reference/sql-statements/programmatic-compound-statements/return.md) can be used instead.

{% hint style="info" %}
`LEAVE` cannot be used to exit a [DECLARE HANDLER](/docs/server/reference/sql-statements/programmatic-compound-statements/declare-handler.md) block.
{% endhint %}

If you try to `LEAVE` a non-existing label, or if you try to `LEAVE` a `HANDLER` block, the following error will be produced:

```sql
ERROR 1308 (42000): LEAVE with no matching label: <label_name>
```

The following example uses `LEAVE` to exit the procedure if a condition is true:

```sql
CREATE PROCEDURE proc(IN p TINYINT)
CONTAINS SQL
`whole_proc`:
BEGIN
   SELECT 1;
   IF p < 1 THEN
      LEAVE `whole_proc`;
   END IF;
   SELECT 2;
END;

CALL proc(0);
+---+
| 1 |
+---+
| 1 |
+---+
```

## See Also

* [ITERATE](/docs/server/reference/sql-statements/programmatic-compound-statements/iterate.md) - Repeats a loop execution

<sub>*This page is licensed: GPLv2, originally from*</sub> [<sub>*fill\_help\_tables.sql*</sub>](https://github.com/MariaDB/server/blob/main/scripts/fill_help_tables.sql)

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
