What is the usage of “@” symbol in MySQL stored procedure?


The @ symbol in a stored procedure can be used for user-defined session variables. Let us first create a table −

mysql> create table DemoTable
(
   StudentName varchar(50)
);
Query OK, 0 rows affected (1.30 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John Smith');
Query OK, 1 row affected (1.00 sec)
mysql> insert into DemoTable values('John Doe');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('Chris Brown');
Query OK, 1 row affected (0.53 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------+
| StudentName |
+-------------+
| John Smith  |
| John Doe    |
| Chris Brown |
+-------------+
3 rows in set (0.00 sec)

Let us now create a stored procedure to calculate the number of records from DemoTable −

mysql> DELIMITER //
mysql> create procedure `Demo_Of_@Symbol`()
   BEGIN
   select count(*) into @numberOfRecords from DemoTable;
   END
//
Query OK, 0 rows affected (0.33 sec)
mysql> DELIMITER ;

Following is the query to call the stored procedure using CALL command −

mysql> call `Demo_Of_@Symbol`();
Query OK, 1 row affected (0.00 sec)

Let us now see the usage of the @symbol −

mysql> select @numberOfRecords;

This will produce the following output −

+------------------+
| @numberOfRecords |
+------------------+
|                3 |
+------------------+
1 row in set (0.00 sec)

Updated on: 30-Sep-2019

804 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements