mysql中的SET用户定义变量是否返回null? [英] SET user defined variable in mysql return null?

查看:122
本文介绍了mysql中的SET用户定义变量是否返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用创建用户定义的变量时

When I created a user defined variable using

SET @a =10; 

我也检查了

SET @a := 10;

以上查询成功执行.在访问变量时,它给出的是NULL而不是10的值.我使用此查询访问了已定义的变量

the above query executed successfully. while accessing the variable it gives me NULL value instead of 10. I accessed the defined variables using this query

SELECT @a;

推荐答案

(在客户端会话中)发生这种情况的唯一方法-以及对我而言不时发生的方法-您会在短时间内感到不安在客户端连接上.它是这样的:

The only way this can happen (in a client session) - and the way it happens for me from time to time - is you get bit by a short timeout on the client connection. It goes like this:

mysql> set @a = 10;

mysql> [wait for N+1 minutes, where N is the client timeout]

mysql> select @a;
+------+
| NULL |
+------+
| NULL | 
+------+
1 row in set (0.00 sec)

您必须初始化变量,并在连续的客户端会话中使用它们.当会话消失时,您将丢失所有变量.

You have to initialize your variables and use them within a contiguous client session. When the session goes away, you lose all your variables.

注释中的其他人指出,另一种解释是命令从不同的连接访问服务器.您的问题可能不是超时,而是您在不同的连接中发起了"SET ..."和"SELECT ..."命令.用户变量不会在不同的连接之间共享.

The other explanation, as pointed out by others in the comments, is that the commands are hitting the server from different connections; your problem might not be a timeout, but that you are originating the "SET ..." and "SELECT ..." commands in different connections. User variables are not shared across different connections.

这篇关于mysql中的SET用户定义变量是否返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆