In another question I posted someone told me that there is a difference between:
@variable
and:
variable
in MySQL. He also mentioned how MSSQL has batch scope and MySQL has session scope. Can someone elaborate on this for me?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
MySQL has a concept of user-defined variables.
They are loosely typed variables that may be initialized somewhere in a session and keep their value until the session ends.
They are prepended with an
@sign, like this:@varYou can initialize this variable with a
SETstatement or inside a query:When you develop a stored procedure in MySQL, you can pass the input parameters and declare the local variables:
These variables are not prepended with any prefixes.
The difference between a procedure variable and a session-specific user-defined variable is that a procedure variable is reinitialized to
NULLeach time the procedure is called, while the session-specific variable is not:As you can see,
var2(procedure variable) is reinitialized each time the procedure is called, while@var2(session-specific variable) is not.(In addition to user-defined variables, MySQL also has some predefined "system variables", which may be "global variables" such as
@@global.portor "session variables" such as@@session.sql_mode; these "session variables" are unrelated to session-specific user-defined variables.)