What's the difference between accessing a variable with @
or without?

- 21,122
- 10
- 69
- 105
-
1I'm not MySql guy, but I want to learn. As far as I can see from the documentation, variables are defined as having an "@" the same as with SQL Server. The "@" is what tells the engine it's a variable name. I'm not sure how you could access a variable without the @. Can you clarify? Perhaps show code samples where you use the "@" and where you don't? http://dev.mysql.com/doc/refman/5.0/en/user-variables.html – David May 09 '12 at 14:24
-
already answered on stack overflow chk this out http://stackoverflow.com/questions/361747/what-does-the-symbol-do-in-sql – Amogh Rai May 09 '12 at 14:26
2 Answers
The @
makes it a user defined session variable. Otherwise it would be locally scoped variable (in a stored procedure), you would have to DEFINE
your local before you can SET
it. You could also set a global system variable (with SET GLOBAL
or SET @@global
) if you wanted to. As well as a session system variable with SET SESSION var
or SET @@session var
or SET @@var
.
More details about SET
from the documentation: If no modifier is present, SET
changes the session variable (that's why you DEFINE
your locals in the stored procedure first). If you set several system variables, the most recent GLOBAL
or SESSION
modifier in the statement is used for following variables that have no modifier specified.
More (and some good examples) here:

- 1
- 1

- 6,085
- 1
- 26
- 24
-
Thanks! That answers my question on the comment above as well as the original OP's question! +1. – David May 09 '12 at 14:33
That notation is used for user-defined variables, as explained here: http://dev.mysql.com/doc/refman/5.0/en/user-variables.html

- 170,779
- 38
- 263
- 309