The error message in this case tells the most important part:
Not allowed to return a result set from a function
This behaviour is consistent with what's documented in MySQL manual on stored procedures and functions:
For statements that can be determined at function definition time to return a result set, a Not allowed to return a result set from a function error occurs (ER_SP_NO_RETSET).
You assign values to your @Pn
variables using select statements that return a resultset and this is not allowed in a function. You have to remove these statements from your code. RETURN river
returns a result value, but not a result set.
I'm also worried that you use session variables (variables defined as @variable_name) which are shared across a connection, so potentially multiple call to the same function at the same time within a connection may interfere with each other.
A stored function is only supposed to return a single value as its output in with the return
statement. Anything else is considered as a side effect. If you want your MySQL script to populate multiple variables, then you must use a stored procedure, you cannot use a stored function.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…