What does `#` mean when not appending any variable name? - mysql

Playing around with MariaDB I accidentally found out that it's possible to use # without giving a variable name. I executed following statement:
SELECT # INTO #;
I would have expected a syntax error since the variable name was omitted, but instead it executes just fine. Now I'm wondering what is happening.
How does MariaDB interpret the # symbol in this case? What does this SELECT actually do? Or is it just completely ignored without any further operation?

Although neither the MySQL nor MariaDB documentation specifically mentions it, apparently user-defined variable are allowed to have an empty name. # is the same as #'', and it's treated just like any other variable.
So
SELECT # INTO #;
is like
SELECT #myvar INTO #myvar;
It's a useless statement, since it's just assigning a variable to itself, equivalent to
SET # = #;

Related

What is the correct form of declaring variables in MySQL: declare vs # vs set? [duplicate]

This question already has answers here:
MySQL: #variable vs. variable. What's the difference?
(5 answers)
Closed 2 years ago.
I am trying to learn MySQL and after doing some database, I am having the following questions.
When declaring a variable inside a procedure or in the function I can do it like this:
using # sign, like this: SET #varname = varcontent
using DECLARE keyword, like this: DECLARE varname vartype
using directly SET without DECLARE, like this SET varname = varcontent
My doubts are:
what type of syntax is correct, using DECLARE, # or SET?
what would be the correct form of doing it?
Also, I would like if you could recommend some good practices with MySQL.
varname and #varname are two DIFFERENT variables!
#varname - user-defined variable.
Exists always. Even when it was not initialized and/or used, in such case it has NULL value. I.e. it does not need in declaration.
Have no datatype (or it has dynamic datatype). Datatype may be easily changed by assigning the value of another datatype.
This variable has a scope of a connection. I.e. it exists until the connection exists, and its value is not altered until it is altered explicitly. Each connection has its own variavle with the same name, they do not interfere.
For example, you may set it to some value, then use/alter this value in called stored procedure, then use the value altered in the procedure in outer code after the procedure finished.
varname - local (declared) variable
Not exists in anonymous code, exists only within compound (BEGIN-END) code block. Must be declared explicitly at the beginning of the block. Is destroyed at the end of the block. Special type of local variable is function/procedure parameter - it is declared in function/procedure header and exists within the function/procedure code block.
Has definite datatype. Cannot be re-declared.
Has a scope of a block where it is defined.
If a variable and a column with the same name exists in some scope, then the variable has priority and masks the column, so if you need to access the column you must specify table alias.
In most constructions any variable type may be used. But sometimes only one of types may be used - consult User Manual.
1.using # like set #varname= varcontent
This statement initializes user-defined variable with some value.
2.using declare like declare varname vartype
This statement declares local variable. May posess at the beginning of BEGIN-END block. Does not set a value to the variable (it is NULL).
3.using directly set varname=varcontent without declare
Causes an error.

Set variable in mysql sql:query

I have a SET variables followed by a SELECT which works fine with MySQL until I attempt this wrapped in a Java query sql:query (which then indicates a syntax error on `SET). I removed the chevrons around the sql:query statement so you can see it.
sql:query var="queryresults" dataSource="jdbc/logs"
SET #locationID=0, #ts=NULL, #changed=0;
SELECT
L4.assetid, etc etc
Is there a different command to SET variables for sql:query or do I need to set these variables beforehand, outside of sql:query?

use of ':=' fails in select in stored procedure due to variable definition

Why does the use of the assignment operator := fail to parse in this stored procedure (fragment)? In the update statement, in the set median = [select expression], in the expression, the MySQL 5.6 parser reports the error, "[Check]...for the right syntax to use near ':= row + 1 as row, $vol as $vol from univ_members' ".
delimiter //
CREATE PROCEDURE m()
BEGIN
DECLARE row int;
SELECT row := row + 1 AS row;
END //
delimiter ;
Running the select statement the mysql shell also fails, but says, 'row' is not a system variable or 'row' is not a column, depending on whether I try to define it with 'set'.
Do you know of a limitation in a stored procedure that prohibits this, or of such a bug in MySQL 5.6? If so, is there a workaround? Can you suggest an alternative approach?
So, after struggling like a man blinded by darkness, I defined the variable #row in the shell using 'set' (the shell's parser does not allow 'row') and it worked. The parser however does not allow a variable defined in a stored procedure with 'declare' to begin with a '#', but, if defined with 'set', it works, it does allow it to be used as the left-hand value in the :=.
So, it's an issue with variable definition. Evidently, only 'user variables', which must begin with a '#' and must be defined with 'set', can be assigned values with ':='. (See User-Defined Variables)
I find such a nuance that all variables don't share a common behavior when it comes to assignment non-intuitive and incredibly irritating. Am I still missing something?

MySQL: What does the equal colon =: mean in an update statement?

I came across the following MySQL query:
update 'table' set itemId=:itemId, startDate=:startDate where id=:id
However I could not figure out what the =: means. I think that the name after the =: is a variable but then how to check what's inside or how is it set?
That is likely referencing a bind variable. The PHP or other code that executes the MySQL statement replaces the reference with a variable.

`#` Symbol in mysql variable names

I have started maintaining a bunch of mysql stored procs. Some variables (created with decalare statements) are accessed with the # symbol and others without it. Whats the difference
A variable with the # at the beginning is session variable. It exists until the session end.