
- 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
MySQL stored procedure to return a column value?
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int, -> Score int -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(1,858858686); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(2,9900554); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(3,646565667); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+------+-----------+ | Id | Score | +------+-----------+ | 1 | 858858686 | | 2 | 9900554 | | 3 | 646565667 | +------+-----------+ 3 rows in set (0.00 sec)
Following is the MySQL stored procedure −
mysql> DELIMITER // mysql> CREATE PROCEDURE Test_StoredProcedure(in id INT, OUT scoreValue INT) -> BEGIN -> SELECT Score -> INTO scoreValue -> FROM DemoTable tbl -> WHERE tbl.Id = id; -> END -> // Query OK, 0 rows affected (0.18 sec) mysql> DELIMITER ;
Now, call the stored procedure with the help of call command and store the output into a variable name called ‘@result’ −
mysql> call Test_StoredProcedure(2,@result); Query OK, 1 row affected (0.00 sec)
Now display the variable value using select statement −
mysql> select @result;
Output
This will produce the following output −
+---------+ | @result | +---------+ | 9900554 | +---------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL stored procedure return value?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- MySQL Stored Procedure to create a table?
- MySQL stored-procedure: out parameter?
- Set conditions in a MySQL stored procedure
- How to suppress MySQL stored procedure output?
- How to loop thrugh a stored procedure in MySQL?
- Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values
- Display description of MySQL stored procedure
- 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?
- Perform mathematical operations in a MySQL Stored Procedure?
- How to quit/ exit from MySQL stored procedure?

Advertisements