MySQL: @variable vs. variable. Whats the difference?

MySQL: @variable vs. variable. Whats the difference?


up vote351down votefavorite

121

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?

 
add a comment

up vote445down voteaccepted

MySQL has the 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: @var

You can initialize this variable with a SET statement or inside in a query:

SET @var = 1

SELECT @var2 := 2

When you develop a stored procedure in MySQL, you can pass the input parameters and declare the local variables:

DELIMITER //

CREATE PROCEDURE prc_test (var INT)
BEGIN
    DECLARE  var2 INT;
    SET var2 = 1;
    SELECT  var2;
END;
//

DELIMITER ;

These variables are not prepended with any prefixes.

The difference between a procedure variable and a session-specific user-defined variable is that procedure variable is reinitialized to NULL each time the procedure is called, while the session-specific variable is not:

CREATE PROCEDURE prc_test ()
BEGIN
    DECLARE var2 INT DEFAULT 1;
    SET var2 := var2 + 1;
    SET @var2 := @var2 + 1;
    SELECT  var2, @var2;
END;

SET @var2 = 1;

CALL prc_test();

var2  @var2
---   ---
2     2

CALL prc_test();

var2  @var2
---   ---
2     3

CALL prc_test();

var2  @var2
---   ---
2     4

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.port or "session variables" such as @@session.sql_mode; these "session variables" are unrelated to session-specific user-defined variables.)

时间: 2024-10-03 14:14:43

MySQL: @variable vs. variable. Whats the difference?的相关文章

MySQL:unknown variable 'master-host=masterIP' [ERROR] Aborting

<span style="font-size:18px;">120401 15:45:44 [ERROR] C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld: unknown variable 'master-host=192.168.8.111' 120401 15:45:44 [ERROR] Aborting --------解决方式-------------------- Mysql版本号从5.1.7以后開始就不支持

MySQL:unknown variable &#39;master-host=masterIP&#39; [ERROR] Aborting

<span style="font-size:18px;">120401 15:45:44 [ERROR] C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld: unknown variable 'master-host=192.168.8.111' 120401 15:45:44 [ERROR] Aborting --------解决方案-------------------- Mysql版本从5.1.7以后开始就不支持&

mysql:unknown variable &#39;default-character-set=utf8&#39;

1.修改my.cnf后,执行 service mysql restart 重启数据库失败 service mysql restart Shutting down MySQL.. SUCCESS! Starting MySQL... ERROR! The server quit without updating PID file (/usr/local/mysql/data/VM_0_12_centos.pid) 2.查看日志: cat VM_0_12_centos.err |grep ERROR

mysql: [ERROR] unknown variable &#39;datadir=/var/lib/mysql&#39;问题

环境: Centos7,mysql 5.7 问题: 在使用命令"mysql -u root -p"连接mysql时,报:"mysql: [ERROR] unknown variable 'datadir=/var/lib/mysql'". 分析: 网上出现这个问题很少,通过类似问题,发现这个问题跟"my.cnf"配置有关,该配置文件在"/etc/my.cnf". 在使用mysql命令连接时,需要获得[client]参数,而da

MySQL: Set user variable from result of query

set @user = 123456;set @group = (select GROUP from USER where User = @user);select * from USER where GROUP = @group; SET @user := 123456;SELECT @group := `group` FROM user WHERE user = @user;SELECT * FROM user WHERE `group` = @group; SET @user := 123

Tensorflow动态seq2seq使用总结(r1.3)

https://www.jianshu.com/p/c0c5f1bdbb88 动机 其实差不多半年之前就想吐槽Tensorflow的seq2seq了(后面博主去干了些别的事情),官方的代码已经抛弃原来用静态rnn实现的版本了,而官网的tutorial现在还是介绍基于静态的rnn的模型,加bucket那套,看这里. tutorial.png 看到了吗?是legacy_seq2seq的.本来Tensorflow的seq2seq的实现相比于pytorch已经很复杂了,还没有个正经的tutorial,哎

mac os x mysql 出现./mysql: unknown variable &#39;sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABL 问题

装完数据库之后,最开始都还好好的,隔了几天发现打不开了 点击start 没有反应,然后通过终端在mysql 安目录下 运行./mysql 出现: unknown variable 'sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABL,最后在/etc/my.cnf 中,把 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABL 删掉或者注释掉,发现问题解决了. mac os x mysql 出现./mysq

Machine Learning - II. Linear Regression with One Variable (Week 1)

http://blog.csdn.net/pipisorry/article/details/43115525 机器学习Machine Learning - Andrew NG courses学习笔记 单变量线性回归Linear regression with one variable 模型表示Model representation 例子: 这是Regression Problem(one of supervised learning)并且是Univariate linear regressi

unknown variable &#39;character-set-server=utf8&#39;

登录mysql出现"unknown variable 'character-set-server=utf8"的错误提示, 这个时候应该检查你的my.ini配置文件: 将[mysql]和[mysqld]下的 character-set-server=utf8 改为 default-character-set=utf8 再重新登录. unknown variable 'character-set-server=utf8'