Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 February MySQL: Procedure Variable vs Session specific User Defined Variable (@variable vs variable)

MySQL: Procedure Variable vs Session specific User Defined Variable (@variable vs variable)

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

I found lots of questions and discussions on the difference between stored procedure local variable and session-specific user-defined variable of MySQL.

MySQL provides session specific user defined variable to initialize and use during the session.

The scope of this variable is for the specific session only and outside of that session another client connection cannot access this variable and once session terminates, it destroys automatically.

This is loosely typed variables and we can initialize somewhere in a session.
We can write @var_name to define a session specific variable.

We also have one local variable, which we can define and use for specific stored objects like a stored procedure.
The scope of this local variable is for that object execution purpose. Once a stored procedure executes, it destroys automatically.

Below is a small demonstration on use of @variable vs variable:
Create a sample stored procedure with session and local variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DELIMITER //
CREATE PROCEDURE usp_TestVariables()
BEGIN
-- Declare Local Variable.....
DECLARE MyVariable INT DEFAULT 1;
SET MyVariable := MyVariable + 1;
-- Define Session Specific Variable.....
SET @MyVariable := @MyVariable + 1;
SELECT MyVariable, @MyVariable;
END;
 
-- Assign value to user defined variable.
SET @MyVariable = 1;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-- Execute this stored procedure multiple times to check a value of both the variables.
CALL usp_TestVariables();
 
--The result of first call.
MyVariable @MyVariable
----------- ----------------
2 2
 
--The result of second call.
MyVariable @MyVariable
----------- ----------------
2 3
 
--The result of third call.
MyVariable @MyVariable
----------- ----------------
2 4
 
--The result of fourth call.
MyVariable @MyVariable
----------- ----------------
2 5

You can check the above result, where @MyVariable is incrementing because it persists values until session end.
A stored procedure local variable is not incrementing because it destroyed after every execution of the stored procedure.

Feb 17, 2016Anvesh Patel
PostgreSQL 9.4: Indexing on jsonb Data Type (Part 3/3)SQL Server: Difference between Temp Table and Common Table Expression (CTE)
Comments: 4
  1. Rasheeda Cogbill
    March 20, 2016 at 7:45 pm

    Hello. magnificent job. I did not expect this. This is a great story. Thanks!

  2. ketopower
    December 12, 2018 at 1:33 am

    We absolutely love your blog and find most of your post’s to
    be precisely what I’m looking for. Would you offer guest writers to write
    content for you? I wouldn’t mind composing a post or elaborating
    on a few of the subjects you write with regards to here.

    • Anvesh Patel
      Anvesh Patel
      December 24, 2018 at 7:35 pm

      Sure, will open very soon

  3. Keshav
    September 27, 2019 at 2:53 am

    Is there a difference how session variables are stored/ treated in storage layer.
    Are there any advantage/disadvantage in using these?

Anvesh Patel
Anvesh Patel

Database Engineer

February 17, 2016 MySQLAnvesh Patel, database, database research and development, dbrnd, local variable, MySQL, MySQL Command, MySQL Database Administrator, MySQL Database Designing, MySQL Database Programming, MySQL Error, MySQL Performance Tunning, MySQL Query, MySQL Tips and Tricks, session variable, user defined variable
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....