User Defined Variables vs Local Variables in MySQL
Last Updated :
23 Jul, 2025
In MySQL, both user-defined and local variables are used to hold temporary data while running SQL queries. User-defined variables, marked with '@', are handy for transferring values between different queries within a session.
Meanwhile, local variables are declared using DECLARE inside stored procedures and have a more limited scope, making them useful for keeping data confined and organized within specific parts of code. Knowing when to use each type can greatly improve how efficiently and effectively SQL queries are executed.
User-Defined Variables
The @ sign precedes user-defined variables in MySQL, which may be set and retrieved at any time throughout a session. They are commonly used to temporarily store values and transfer them across queries.
Syntax:
-- Set a User-Defined Variable
SET @user_var = value;
-- Access the User-Defined Variable
SELECT @user_var;
Parameters:
- Declaration: Prefixed with @ symbol (e.g., @user_var).
- Scope: Session-wide; accessible across multiple queries.
- Usage: Suitable for storing temporary values and passing between queries.
Local Variables
In MySQL, local variables are declared with the DECLARE command within a stored program (such as a stored procedure or function). They have a specific scope inside the block in which they are stated.
Syntax:
-- Declare and Use a Local Variable in a Stored Procedure
DELIMITER //
CREATE PROCEDURE example_procedure()
BEGIN
DECLARE local_var INT DEFAULT 50;
-- Accessing the Local Variable
SELECT local_var;
END //
DELIMITER ;
Parameters:
- Declaration: Declared using DECLARE within stored programs.
- Scope: Limited to the block where they are declared (e.g., stored procedure).
- Usage: Ideal for encapsulating logic within a specific routine, ensuring data integrity.
Example of User-defined Variables and Local Variables in MySQL
Example 1: User-Defined Variable
-- Schema and Sample Data
CREATE TABLE sales (
product VARCHAR(50),
amount INT
);
INSERT INTO sales VALUES ('Laptop', 1000);
INSERT INTO sales VALUES ('Desktop', 800);
INSERT INTO sales VALUES ('Tablet', 500);
-- Setting a User-Defined Variable
SET @total_sales = 0;
-- Updating the User-Defined Variable
UPDATE sales SET amount = amount + @total_sales;
-- Displaying the Result
SELECT product, amount, @total_sales AS running_total
FROM sales;
Output:
product
| amount
| running_total
|
|---|
Laptop
| 1000
| 1000
|
Desktop
| 800
| 1800
|
Tablet
| 500
| 2300
|
Explanation:
- The sales table is generated with columns for product and quantity.
- Sample data is entered into the table.
- A user-defined variable, @total_sales, is set to zero.
- The amount column in the sales database is updated with the value of @total_sales.
- The outcome is displayed, including the running total.
Example 2: Local Variable in a Stored Procedure
-- Schema and Sample Data
CREATE TABLE products (
product VARCHAR(50),
price INT,
stock INT,
discounted_price DECIMAL(8,2)
);
INSERT INTO products VALUES ('Laptop', 1000, 20, NULL);
INSERT INTO products VALUES ('Desktop', 800, 15, NULL);
INSERT INTO products VALUES ('Tablet', 500, 30, NULL);
-- Declaring a Local Variable in a Stored Procedure
DELIMITER //
CREATE PROCEDURE calculate_discounted_price()
BEGIN
DECLARE discount_percent DECIMAL(5,2) DEFAULT 10.00;
-- Updating the Price with Discount
UPDATE products SET discounted_price = price - (price * discount_percent / 100);
-- Displaying the Result
SELECT * FROM products;
END //
DELIMITER ;
-- Calling the Stored Procedure
CALL calculate_discounted_price();
Output:
product
| price
| stock
| discounted_price
|
|---|
Laptop
|
1000
|
20
|
900.00
|
Desktop
|
800
|
15
|
720.00
|
Tablet
|
500
|
30
|
450.00
|
Explanation:
- The products table has four columns: product, price, stock, and discounted_price.
- Sample data is entered into the table.
- A saved procedure called calculate_discounted_price is created.
- Within the procedure, a local variable discount_percent is defined with a default value.
- The discounted_price column in the products table is updated according to the discount computation.
- The results are displayed, including the current discounted pricing.
Conclusion
In Conclusion, MySQL's user-defined variables marked with '@' are handy for storing temporary values across sessions, facilitating data exchange between queries. Conversely, local variables, declared within stored procedures using DECLARE, ensure encapsulation of logic within specific program blocks, promoting data integrity. Understanding these distinctions enables efficient SQL query development, enhancing database operations and overall application performance.
Explore
MySQL Basics
MySQL User Management
MySQL Managing Databases
MySQL Managing Tables
MySQL Query
MySQL Clauses
MySQL Aggregate Functions
MySQL Data Constraints
MySQL Joining Data
MySQL Functions