MySQL Tutorials - Herong's Tutorial Examples - v4.46, by Herong Yang
User-Defined vs. Stored Procedure Variables
This section provides a tutorial example to show you differences between user-defined variables and stored procedure variables: syntax, declaration and scope.
What Are the Differences between User-Defined Variables and Stored Procedure Variables? User-Defined Variables and Stored Procedure Variables are having 3 main differences:
1. Different Syntax: - Stored Procedure Variables can not start with "@". User-Defined Variables must be prefixed with "@".
2. Type Declaration: - Stored Procedure Variables must be declared with specific data types. User-Defined Variables do not need any type declaration.
3. Different Scope: - Stored Procedure Variables are only valid locally inside the stored procedure where are introduced. User-Defined Variables are valid globally in the entire client session.
Here is an example client session showing you the differences between user-defined variables and stored procedure variables.
mysql> SET @PI := 3.14; mysql> DELIMITER '/'; mysql> CREATE PROCEDURE GetArea(IN Radius REAL, OUT Area REAL) -> BEGIN -> DECLARE Square REAL; -> SET Square := Radius*Radius; -> SELECT @PI*Square INTO Area; -> END/ mysql> DELIMITER ';'/ mysql> CALL GetArea(2.0, @area); mysql> SELECT @area; +-------+ | @area | +-------+ | 12.56 | +-------+ mysql> DROP PROCEDURE IF EXISTS GetArea;
Table of Contents
MySQL Introduction and Installation
Introduction of MySQL Programs
Perl Programs and MySQL Servers
Java Programs and MySQL Servers
Character Strings and Bit Strings
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
Window Functions for Statistical Analysis
Use Index for Better Performance
Transaction Management and Isolation Levels
Defining and Calling Stored Procedures
Variables, Loops and Cursors Used in Stored Procedures
►System, User-Defined and Stored Procedure Variables
System Variables Like @@version
Scopes of System Variables: Global and Session
User-Defined Variables Like @x
►User-Defined vs. Stored Procedure Variables
Storage Engines in MySQL Server
InnoDB Storage Engine - Primary and Secondary Indexes
Performance Tuning and Optimization
Installing MySQL Server on Linux