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

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.

Related

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

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 # = #;

How to store a list of IP addresses in SQL variable?

The goal is to query multiple IP addresses. I am using HeidiSQL to run this query.
Error:
SQL Error (1064): You have an error in your SQL syntax;check the manual that corresponds to your MariaDB server version for the right syntax to use near "10.3.22.14" at line 1.
SQL query:
set #trunk='10.3.22.5','10.3.22.14';
set #sdate='2021-06-29 00:00:00', #edate='2021-06-29 23:59:00';
SELECT acctstarttime, acctstoptime, acctsessiontime, calledstationid, callingstationid, username, acctterminatecause, h323disconnectcause, h323calltype, acctstatustype, nasipaddress, nasportid, h323remoteaddress, acctinputoctets, servicetype, acctoutputoctets
FROM radius.radacct
where (acctstarttime between #sdate and #edate )
and nasipaddress in (#trunk);
How can I fix this syntax error?
You can not define a list as variable.
set #trunk='10.3.22.5','10.3.22.14';
Define a comma-separated list, and use FIND_IN_SET.
set #trunk='10.3.22.5,10.3.22.14';
select FIND_IN_SET('10.3.22.5', #trunk);
refer
First, I don't really recommend what you are doing. But to get the correct syntax, you need to realize that ' is a special character in SQL. It is a string delimiter. So:
set #trunk='10.3.22.5','10.3.22.14';
is just two strings next to each other. And MariaDB does not recognize that syntax. What you are trying to do is:
set #trunc = '10.3.22.5,10.3.22.14'
Then, you can use this in the query not with in, but using find_in_set():
where find_in_set(nasipaddress, #trunc) > 0
Note that using the function find_in_set() prevents the use of indexes. So, if your table is large, then another solution is preferable, perhaps by storing the values in a table and using join.
Cannot define list as a variable.
set #trunk="10.3.22.5,10.3.22.14";
select FIND_IN_SET("10.3.22.5", #trunk);

MySQL - SELECT INTO within a Stored Procedure

We have a lot of stored procedures which have an OUT parameter which we are assigning a value to by using SELECT LAST_INSERTID() INTO p_AutoNumber.
After this, we need to perform another INSERT which also uses the value of this variable - however it doesn't appear to run correctly.
Does performing a SELECT INTO on an OUT parameter return from the procedure immedately? I am unable to find any information on this in the MySQL docs for SELECT INTO.
You can use the out-parameter within the procedure. Just check the syntax (and the function name):
Use syntax:
SELECT LAST_INSERT_ID() INTO p_AutoNumber;
Alternatively you can use syntax:
SET p_AutoNumber = LAST_INSERTID();

How do I write an SQL query that fails?

I need this query for testing exception handling, so I would prefer that the query is not schema dependent. I am looking for something like SELECT 1; but of course that doesn't fail.
I am using Java and MySQL but I hope to find answers that doesn't depend on programming languages and/or RDBMSs.
What about "SELECT 1/0" for starters?
You could put an invalid token into the query
select doesnotexist.* from something_else
Or of course, what you should do is mock out the method and have it throw the exception during your test.
there are tons of ways to make a query fail, like mispelling a field, or selecting from non existing tables. for example:
SELECT some_fake_field FROM table_that_doesnt_exists
One way to trigger a failure is to call a stored procedure with the wrong number of parameters. Another similar idea is to write an update/insert statement with the wrong number of arguments...
More ideas here:
How to raise an error within a MySQL function
Any old syntax error will do... like an unterminated string
select 'bob
To get 1/0 to raise an error in MySQL, you need to set sql_mode to ERROR_FOR_DIVISION_BY_ZERO.
Try this:
SET sql_mode = 'ERROR_FOR_DIVISION_BY_ZERO';
SELECT 1/0;
If this sql_mode isn't set, MySQL will return a NULL instead of an error.
You can check what your current settings are with the following:
SELECT ##GLOBAL.sql_mode;
SELECT ##SESSION.sql_mode;

MySql stored procedure SET command -- when it errors

After Googling for awhile I didn't see an answer. Anyway I have a situation in a stored procedure where I do a set select like:
SET someVariable = (SELECT ...)
Anyway, due to some redundant records existing somewhere else in the system, this SELECT query used in the SET returns more than one row. I'm guessing this will cause breakage or badness? True, false?
Thanks.
True. When assigning to a variable, the query must return a single row, containing a single column. You can also do it with this syntax:
SELECT someColumn INTO myVariable ... LIMIT 1;