- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What do you mean by Scope of variables inside MySQL stored procedure?
Suppose if we declare a variable inside a BEGIN/END block then the scope of this variable would be in this particular block. We can also declare a variable with the same name inside another BEGIN/END block which will be totally legal but its scope would be inside its BEGIN/END block. It can be understood with the help of the following example in which we are creating a procedure to show the scope of the variables −
Example
mysql> Create Procedure Scope_variables() -> BEGIN -> DECLARE A Varchar(5) Default 'outer'; -> BEGIN -> DECLARE A Varchar(5) Default 'inner'; -> SELECT A; -> END; -> SELECT A; -> END; -> // Query OK, 0 rows affected (0.08 sec)
In the above procedure, we have two variables with the same name i.e. A. Here the inner variable declaration takes precedence as long as it is in scope. The point is that the inner variable disappears when the first END is reached and it is called ‘OUT OF SCOPE’. To understand the concept invoke this procedure as follows −
mysql> CALL Scope_variables(); +-------+ | A | +-------+ | inner | +-------+ 1 row in set (0.00 sec) +-------+ | A | +-------+ | outer | +-------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec)
- Related Articles
- What do you mean by a dynamic initialization of variables?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Implement Dynamic SQL query inside a MySQL stored procedure?
- Create variables in MySQL stored procedure with DECLARE keyword
- What do you mean by Salmon?
- What do you mean by transpose?
- What do you mean by machine?
- What do you mean by cartilage?
- What do you mean by Mode?
- What do you mean by Silk?
- What do you mean by Sex?
- What do you mean by Virtual ?
- What do you mean by Adaptation?
- What do you mean by thermometer?
- What do you mean by chemistry?

Advertisements