J.L. Yokata
J.L. Yokata

Reputation: 115

MYSQL, assign and read a variable in same statement

LINK: https://dev.mysql.com/doc/refman/5.7/en/user-variables.html

I was reading mysql documentation and I got really confused about that general rule!

Documentation:

As a general rule, OTHER THAN IN SET statements, you should never assign a value to a user variable and read the value within the same statement.

Ok, so does it means that I can use SET statement to assign AND READ a user variable? right? BUT when I try it...

SET @a:=1, @b:=@a+1;
SELECT @a,@b; # @a = 1, @b = NULL

Looks like the SET statement have the same problem of any other statement for assign and read user variables in the same statement.

SET @a:=1;
SET @b:=@a+1;
SELECT @a,@b; # @a=1 , @b=2

So, what am I missing here?

Upvotes: 0

Views: 98

Answers (1)

Barmar
Barmar

Reputation: 780698

I think the documentation is simply wrong. As you've shown, there's not really any difference between SET and SELECT in this regard.

Upvotes: 1

Related Questions