Bug #77777 Documentation Clarification
Submitted: 18 Jul 2015 22:51 Modified: 20 Jul 2015 19:16
Reporter: Gordon Linoff Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Documentation Severity:S3 (Non-critical)
Version:All Versions OS:Any
Assigned to: CPU Architecture:Any
Tags: documentation, variables

[18 Jul 2015 22:51] Gordon Linoff
Description:
The following construct is clearly allowed in MySQL:

    select (@rn := 5) as seqnum

However, nothing in the documentation specifies that this is allowable and consistently assign "5" to seqnum.  Further, the documentation for user defined variables (https://dev.mysql.com/doc/refman/5.7/en/user-variables.html) warns specifically against using the value of a variable in a select:

"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."

The documentation should clearly state that the assignment operation := returns the assigned value.  Then it would be clear that original statement is not "reading" the value after setting it, it is merely using the return value from the statement.

How to repeat:
This is a documentation fix, so it is repeatable by reading the documentation.

Suggested fix:
Something to the effect:

"In a SELECT statement, the := operator returns the value assigned to the value.  The value can be assigned an alias for subsequent processing in the query."
[20 Jul 2015 11:57] MySQL Verification Team
Hello Gordon,

Thank you for the report.
Assignment operator ":=" can be used in any valid SQL statement (not just in SET statements) to assign a value to a variable. This has been explained in the relevant section of manual page https://dev.mysql.com/doc/refman/5.6/en/assignment-operators.html

Thanks,
Umesh
[20 Jul 2015 19:16] Gordon Linoff
Umesh . . . You are missing the point.  Of course `:=` can be used to assign a variable.  The point is that it is an expression that assigns a value.  So:

     select (@x := (@y := 1))

is perfectly legal.  The documentation should clarify this.