
- 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
Assign an SQL result to variable from prepared statement in MySQL?
For this, use stored procedure. Let us first create a table −
mysql> create table DemoTable(Id int, Name varchar(100)); Query OK, 0 rows affected (1.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,'John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(11,'Chris'); Query OK, 1 row affected (0.41 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 10 | John | | 11 | Chris | +------+-------+ 2 rows in set (0.00 sec)
Following is the query to assign SQL result to variable from prepared statement in MySQL −
mysql> DELIMITER // mysql> CREATE PROCEDURE Prepared_Statement_Demo( nameOfTable VARCHAR(20), IN nameOfColumn VARCHAR(20), IN idColumnName INT) BEGIN SET @holdResult=CONCAT('SELECT ', nameOfColumn, ' INTO @value FROM ', nameOfTable, ' WHERE id = ', idColumnName); PREPARE st FROM @holdResult; EXECUTE st; DEALLOCATE PREPARE st; END // Query OK, 0 rows affected (0.20 sec) mysql> DELIMITER ;
Call the stored procedure using call command −
mysql> call Prepared_Statement_Demo('DemoTable','Name',10); Query OK, 0 rows affected, 2 warnings (0.00 sec)
Now check the value of variable @value −
mysql> select @value;
This will produce the following output −
+--------+ | @value | +--------+ | John | +--------+ 1 row in set (0.00 sec)
- Related Articles
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- How to assign the result of a MySQL query into a variable?
- How to select return value from MySQL prepared statement?
- Set user variable from result of query in MySQL?
- Storing value from a MySQL SELECT statement to a variable?
- How to use prepared statement for select query in Java with MySQL?
- How to store Query Result in a variable using MySQL?
- What happens if I will assign a value to a MySQL user variable using a statement that returns multiple rows?
- Assign other value to a variable from two possible values in C++
- Set the result of a query to a variable in MySQL?
- How to executes an SQL SELECT statement in a JSP?
- How to assign a PHP variable to JavaScript?
- How to assign int value to char variable in Java
- How to assign a reference to a variable in C#
- Write MySQL case statement to set custom messages for student’s result

Advertisements