Local Variables in Stored Procedures

This section describes local variables used in stored procedures. DECLARE statements are used to declare local variables.

A stored procedure can have local variables.

To define a local variable, you can use the DECLARE statement:

DECLARE variable data_type [DEFAULT value];

To assign a new value to a variable, you can use the SET statement:

SET variable = expression;

The SELECT statement can also be used to assign values to variables:

SELECT expression, expression, ... INTO variable, variable, ... 
   [FROM clause];

Once a variable is defined, it can be used in any expressions in any statements.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 Perl Programs and MySQL Servers

 PHP Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

Variables, Loops and Cursors Used in Stored Procedures

Local Variables in Stored Procedures

 Execution Flow Control Statements

 ITERATE and LEAVE - Statements to Break Loops

 DECLARE ... CURSOR FOR Select Statements

 Outdated Tutorials

 References

 PDF Printing Version