How to declare a variable in MySQL for a normal query?



You can declare a variable using @anyVariablename which is a session variable. To create a session variable, you need to use SET command.

The syntax is as follows

SET @anyVariableName:=anyValue;

You can declare a local variable using DECLARE command. The syntax is as follows

DECLARE yourVariableName datatype

You can set the default value at the time of creating a variable. The syntax is as follows

DECLARE yourVariableName datatype default ‘yourValue’

Here is the demo of session variable. To understand it, let us create a table.

The query to create a table is as follows

mysql> create table SessionVariableDemo    -> (    -> EmployeeId varchar(10),    -> EmployeeName varchar(30),    -> EmployeeAge int    -> ); Query OK, 0 rows affected (0.70 sec)

Insert some records in the table using insert command. The query is as follows

mysql> insert into SessionVariableDemo values('EMP-101','Carol',30); Query OK, 1 row affected (0.20 sec) mysql> insert into SessionVariableDemo values('EMP-102','John',26); Query OK, 1 row affected (0.20 sec) mysql> insert into SessionVariableDemo values('EMP-103','Bob',25); Query OK, 1 row affected (0.19 sec) mysql> insert into SessionVariableDemo values('EMP-104','Sam',32); Query OK, 1 row affected (0.14 sec) mysql> insert into SessionVariableDemo values('EMP-105','Mike',35); Query OK, 1 row affected (0.11 sec) mysql> insert into SessionVariableDemo values('EMP-106','David',33); Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement. The query is as follows

mysql> select *from SessionVariableDemo;

The following is the output

+------------+--------------+-------------+ | EmployeeId | EmployeeName | EmployeeAge | +------------+--------------+-------------+ | EMP-101    | Carol        |          30 | | EMP-102    | John         |          26 | | EMP-103    | Bob          |          25 | | EMP-104    | Sam          |          32 | | EMP-105    | Mike         |          35 | | EMP-106    | David        |          33 | +------------+--------------+-------------+ 6 rows in set (0.00 sec)

Now, create a session variable using SET command. After that we will use this variable in the query to get all employee records with age greater than 30.

Let us create a session variable using SET command

mysql> set @AgeGreaterThan30:=30; Query OK, 0 rows affected (0.00 sec)

Here is the query that will use the session variable to get the employee records with age greater than 30

mysql> select *from SessionVariableDemo where EmployeeAge > @AgeGreaterThan30;

The following is the output

+------------+--------------+-------------+ | EmployeeId | EmployeeName | EmployeeAge | +------------+--------------+-------------+ | EMP-104    | Sam          | 32 | | EMP-105    | Mike | 35 | | EMP-106    | David | 33 | +------------+--------------+-------------+ 3 rows in set (0.00 sec)
Updated on: 2019-07-30T22:30:24+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements