> 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.md).

# Programmatic & Compound Statements

{% columns %}
{% column %}
{% content-ref url="/pages/0n3XgXdLHtyd425JLH61" %}
[BEGIN END](/docs/server/reference/sql-statements/programmatic-compound-statements/begin-end.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Group multiple SQL statements into a logical block. This construct defines a compound statement, creating a new scope for variables and exception handling.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/wXjVhZxresTmXzUSa2Jg" %}
[CASE Statement](/docs/server/reference/sql-statements/programmatic-compound-statements/case-statement.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Complete reference for CASE Statement in MariaDB. Complete syntax guide with all options, clauses, and practical examples with comprehensive examples and.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/tItE45CFJuEDkM7zcRRB" %}
[DECLARE CONDITION](/docs/server/reference/sql-statements/programmatic-compound-statements/declare-condition.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Define named error conditions. This statement associates a name with a specific SQLSTATE or MariaDB error code for easier handling in stored programs.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/VxlYld0BVbx8INBSzAg9" %}
[DECLARE HANDLER](/docs/server/reference/sql-statements/programmatic-compound-statements/declare-handler.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Specify actions for error conditions. This statement defines handler routines (CONTINUE or EXIT) to manage exceptions or warnings within a block.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/JzxI2eDNzZiqq9gN1lXH" %}
[DECLARE TYPE](/docs/server/reference/sql-statements/programmatic-compound-statements/declare-type.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Define data types for Oracle compatibility. This statement allows declaring PL/SQL-style record types and associative arrays, and REF CURSOR types within stored procedures.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/mTd8dQSglwtoG5XdJZgp" %}
[DECLARE Variable](/docs/server/reference/sql-statements/programmatic-compound-statements/declare-variable.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Initialize local variables within a stored program. This statement defines variables with a specific data type and optional default value.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/PJ5EfbF0rdsjkvBlOnxr" %}
[FOR](/docs/server/reference/sql-statements/programmatic-compound-statements/for.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Loop through a range or cursor result set. This control flow statement repeatedly executes a block of code for each item in a specified range or query.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/ogrvhg1NYRq92tUW9nkn" %}
[GOTO](/docs/server/reference/sql-statements/programmatic-compound-statements/goto.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Jump to a labeled point in the code. This Oracle-compatible statement transfers execution control to a specific label within the stored program.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/duZD282fYHKuTYOTBhUj" %}
[IF](/docs/server/reference/sql-statements/programmatic-compound-statements/if.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Execute code based on conditions. This control flow statement runs different blocks of SQL statements depending on whether a specified condition is true.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/zVeRSbAYFvz3eq6tP7E0" %}
[ITERATE](/docs/server/reference/sql-statements/programmatic-compound-statements/iterate.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Restart the current loop. This statement jumps back to the beginning of a LOOP, REPEAT, or WHILE block, skipping any remaining statements in the current iteration.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/kEy1Qz2pJaiISB2ToHef" %}
[Labels](/docs/server/reference/sql-statements/programmatic-compound-statements/labels.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Identify blocks and loops for flow control. Labels provide names for BEGIN...END blocks or loops, allowing them to be targeted by LEAVE, ITERATE, or GOTO statements.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/Tw529kdkvfkUwHVFQPCv" %}
[LEAVE](/docs/server/reference/sql-statements/programmatic-compound-statements/leave.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Exit a labeled block or loop immediately. This statement terminates the execution of the current loop or compound statement and continues after the block.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/eBvBgciNMis78vF1iWwH" %}
[LOOP](/docs/server/reference/sql-statements/programmatic-compound-statements/loop.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Create a simple loop construct. This statement repeatedly executes a block of code until explicitly terminated by a LEAVE statement.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/bMYKFe9QmBI7Yweug7oB" %}
[REPEAT LOOP](/docs/server/reference/sql-statements/programmatic-compound-statements/repeat-loop.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Execute a block until a condition is met. This loop construct runs at least once and continues repeating as long as the UNTIL condition remains false.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/mFoj3hVPj7ziX2AUeFU6" %}
[RESIGNAL](/docs/server/reference/sql-statements/programmatic-compound-statements/resignal.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Propagate error conditions. This statement allows a handler to pass an error condition back to the caller or modify the error information before passing it on.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/BVEhsMNDoYWOGVzDnp8H" %}
[RETURN](/docs/server/reference/sql-statements/programmatic-compound-statements/return.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Exit a stored function and return a value. This statement terminates function execution and sends the specified result back to the caller.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/XRRMCxKvb9ZG3be3I6aK" %}
[SELECT INTO](/docs/server/reference/sql-statements/programmatic-compound-statements/selectinto.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Assign query results to variables. This statement retrieves column values from a single row and stores them in local variables or user-defined variables.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/a1e3w74awNvJhQSUcFp7" %}
[SIGNAL](/docs/server/reference/sql-statements/programmatic-compound-statements/signal.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Raise a custom error condition. This statement allows stored programs to generate specific error messages and SQLSTATEs to handle application logic exceptions.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/GLzS0QUl5iE4TRaUeISz" %}
[WHILE](/docs/server/reference/sql-statements/programmatic-compound-statements/while.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Execute a block while a condition is true. This loop construct checks a condition before each iteration and repeats the block as long as the condition holds.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/A8w8ux8suFq9RXituRVe" %}
[Using Compound Statements Outside of Stored Programs](/docs/server/reference/sql-statements/programmatic-compound-statements/using-compound-statements-outside-of-stored-programs.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Use compound statements such as IF and BEGIN...END outside of stored programs, with the delimiter set appropriately.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/LsvIDBh9MnvCwYvy3dad" %}
[Cursors](/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-cursors.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Learn about cursors in MariaDB Server's programmatic compound statements. This section details how to iterate over result sets row-by-row within stored procedures and functions.
{% endcolumn %}
{% endcolumns %}

{% columns %}
{% column %}
{% content-ref url="/pages/dBOYidJAuvU8bAUUZ3Ej" %}
[Diagnostics](/docs/server/reference/sql-statements/programmatic-compound-statements/programmatic-compound-statements-diagnostics.md)
{% endcontent-ref %}
{% endcolumn %}

{% column %}
Learn about diagnostics in programmatic compound statements. This section covers error handling and information retrieval within stored procedures and functions for effective debugging.
{% endcolumn %}
{% endcolumns %}
