
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Store a column's value into a MySQL stored procedure’s variable
To declare a variable, use DECLARE in a MySQL stored procedure. Let us first create a table −
mysql> create table DemoTable2034 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20), -> StudentAge int -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2034(StudentName,StudentAge) values('Chris',23); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2034(StudentName,StudentAge) values('David',21); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable2034(StudentName,StudentAge) values('Robert',25); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable2034(StudentName,StudentAge) values('Mike',19); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2034;
This will produce the following output −
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Chris | 23 | | 2 | David | 21 | | 3 | Robert | 25 | | 4 | Mike | 19 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
Here is the query to create a stored procedure and store the above table’s column value in a stored procedure variable −
mysql> delimiter // mysql> create procedure select_into_variable(id int) -> begin -> declare name varchar(50); -> select StudentName into name from DemoTable2034 where StudentId=id; -> select concat('Your Name is= ',name); -> end -> // Query OK, 0 rows affected (0.09 sec) mysql> delimiter ;
Call the stored procedure −
mysql> call select_into_variable(4);
This will produce the following output −
+-------------------------------+ | concat('Your Name is= ',name) | +-------------------------------+ | Your Name is= Mike | +-------------------------------+ 1 row in set (0.04 sec) Query OK, 0 rows affected (0.06 sec)
- Related Articles
- How to store query result (a single document) into a variable?
- Store a variable with the result of a MySQL SELECT CASE?
- How can we store a value in user-defined variable?
- How to store Query Result in a variable using MySQL?
- Select into a user-defined variable with MySQL
- Check for NULL or empty variable in a MySQL stored procedure
- MySQL stored procedure to return a column value?
- How do I avoid the variable value in a MySQL stored procedure to change when records are updated?
- Why should we not store a number into a MySQL ENUM column?
- Storing value from a MySQL SELECT statement to a variable?
- MySQL “not a variable or NEW pseudo-variable” message. What is this error in my Stored Procedure?
- MySQL stored procedure return value?
- Convert a stored MD5 string to a decimal value in MySQL?
- In MySQL stored procedures, how to check if a local variable is null?
- How can we write MySQL handler, in a stored procedure, that sets the particular value of a variable and continues the execution?

Advertisements