

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- MySQL stored procedure to return a column value?
- MySQL stored procedure return value?
- Check for NULL or empty variable in a MySQL stored procedure
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- MySQL Stored Procedure to create a table?
- Set conditions in a MySQL stored procedure
- Why should we not store a number into a MySQL ENUM column?
- Stored procedure using variable in LIMIT expression?
- How do I avoid the variable value in a MySQL stored procedure to change when records are updated?
- Update a column based on another MySQL table’s column
- How can we add day/s in the date stored in a column of MySQL table?
- How can we alter a MySQL stored procedure?
- How can we drop a MySQL stored procedure?
- Create a stored procedure with delimiter in MySQL
- Implement Conditional MySQL Query in a stored procedure?
Advertisements