> 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" %}
