Limitations on stored programs
The limitations described in this topic apply to all stored routines, that is, stored procedures and stored functions. Some limitations apply only to stored functions and not to stored procedures. The limitations of stored functions also apply to triggers, but triggers have their own limitations.
Limitations on the use of SQL statements
The following limitations apply to the use of SQL statements in stored programs:
-
Stored routines cannot use the
LOAD DATAstatement. -
SQL prepared statements (
PREPARE,EXECUTE, andDEALLOCATE PREPARE) can be used in stored procedures but not in stored functions or triggers. Therefore, stored functions and triggers cannot use dynamic SQL. -
Stored programs also cannot use SQL prepared statements that are restricted in the use of prepared statements. However, the exception handling statements
SIGNAL,RESIGNAL, andGET DIAGNOSTICSare allowed in stored programs, even though they cannot be used as prepared statements. -
Local variables are only effective during the execution of the stored program, so they cannot be referenced by prepared statements created within the stored program. Prepared statements are only valid for the current session, not the stored program they are in, so they can be executed after the program has finished, at which point the variables are no longer in scope.
-
In all stored programs (stored procedures and functions, and triggers), the parser treats
BEGIN[WORK]as aBEGIN...ENDblock. To start a transaction in this context, useSTART TRANSACTION.
Limitations on stored functions
The following additional statements and operations are not allowed in stored functions:
-
Statements that perform an explicit or implicit commit or rollback. SQL standards do not require support for these statements, and the standard allows each DBMS vendor to decide whether to restrict them.
-
Statements that return result sets, such as
SELECTstatements without anINTO var_listclause, andSHOW,EXPLAIN, andCHECK TABLE. Functions can use theSELECT INTOclause or use cursors andFETCHstatements to handle result sets. -
The
FLUSHstatement. -
Stored functions cannot be used recursively.
-
If a table exists in the calling statement of the function or trigger (used for reading or writing), the stored function or trigger cannot modify the table.
These limitations apply to statements in stored procedures, but not to stored procedures called from stored functions or triggers. For example, if a stored procedure uses the FLUSH statement, it cannot be called from a stored function or trigger.
Limitations on triggers
Triggers have the following additional limitations:
-
Foreign key operations do not activate triggers.
-
Triggers cannot return values, so they cannot contain a
RETURNstatement. To exit a trigger immediately, use theLEAVEstatement.
Name conflicts in stored routines
Routine parameters, local variables, and table column names can use the same identifier, and the same local variable name can be used in nested blocks. In cases of ambiguous identifiers, the following priority rules apply:
-
Local variables generally take precedence over routine parameters or table column names.
-
Routine parameters take precedence over table column names.
-
Local variables in an inner block take precedence over local variables in an outer block.