I have a confusion regarding defining variables in stored procedure . We can define variable in store procedure in two different ways …
DECLARE var1
SET @var1
I am confused because …
(a)why there are two ways to define a variable ?
(b)I’m not clear when to use DECLARE and when to use SET . Can you please explain the difference ? Is there any thumb rule when to use which variable ?
Is this MySQL? In MySQL the variable var1 and @var1 are two different things. @var1 is a user session variable which remains for the duration of the active session. The other is local to the stored procedure. In MySQL you need to use SET, SELECT … INTO var or FETCH … INTO for setting local or session variables.
a store procedure can have multiple BEGIN END block.
I guess by local you mean the var is accessible in the block where it has been defined…whereas a user session variable can be accessible in all blocks irrespective of where it has been defined.
Please correct me if I’m wrong. I hope I understood you correctly.