# LEAVE

## Syntax

```sql
LEAVE label
```

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

{% hint style="info" %}
`LEAVE` cannot be used to exit a [DECLARE HANDLER](https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/declare-handler) 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](https://mariadb.com/docs/server/reference/sql-statements/programmatic-compound-statements/iterate) - 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: 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/leave.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.
