mysql set类型的用户变量,mysql中的SET用户定义变量是否返回null?

When I created a user defined variable using

SET @a =10;

also i checked

SET @a := 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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值