How to display the value of a variable on command line in MySQL?


To display the value of a variable, you can use select statement. The syntax is follows −

SELECT @yourVariableName;

Let us first create a variable. This can be done using SET command. The following is the syntax to create a variable −

SET @yourVariableName = yourValue;

Let us check the above syntax to create a variable and display the value of created variable.

The following is the query to create a variable −

mysql> set @FirstName = 'Bob';
Query OK, 0 rows affected (0.04 sec)

Now you can display the value of a variable using select statement. The query is as follows −

mysql> select @FirstName;

The following is the output −

+------------+
| @FirstName |
+------------+
| Bob        |
+------------+
1 row in set (0.03 sec)

Let us see another example and declare a real type with value −

mysql> set @Amount = 150.56;
Query OK, 0 rows affected (0.00 sec)

Display value using select statement −

mysql> select @Amount;

The following is type output displaying the value of the variable −

+---------+
| @Amount |
+---------+
| 150.56  |
+---------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements