Comments - IF

5 years, 10 months ago Roberto Blandino

I am using "Server version: 10.1.35-MariaDB-1stretch mariadb.org binary distribution".

The documentation does not provide any functional example, I am trying to run a simple test because i need a complex approach but before implement the conditional i did a simple test following the documentation and i got this

MariaDB [(none)]> IF 1=1 THEN SELECT 1 END IF; ERROR 1064 (42000): 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 'IF' at line 1

But when I test adding ";" on the syntax:

MariaDB [(none)]> IF 1=1 THEN; SELECT 1; END IF; ERROR 1064 (42000): 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 '' at line 1 +---+

1

+---+

1

+---+ 1 row in set (0.00 sec)

ERROR 1064 (42000): 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 'END IF' at line 1

Same when trying with a functional example:

MariaDB [dbgpon]> set @myId = (SELECT IF ((SELECT count(1) FROM `vlan` WHERE `gpon` = 1 AND `value`='1') = 1, TRUE, FALSE)); Query OK, 0 rows affected (0.00 sec)

MariaDB [dbgpon]> IF @myId=1 THEN; SELECT @myId; ERROR 1064 (42000): 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 '' at line 1 +-------+

@myId

+-------+

1

+-------+ 1 row in set (0.00 sec)

MariaDB [dbgpon]>

I hope a better documentation and explain what does the message means:

right syntax to use near '' at line 1<<

Where is '' ?

 
5 years, 7 months ago Russ Kubes

Roberto,

I was having this same issue, then was able to figure it out.

I was using a script to create a SPROC that contained the IF statement, and every time I got the same error you did.

I then used the "DELIMITER $$" statement at the top of my script. Then I changed the ';' on every statement outside of the SPROC that was defining. As an example:

DELIMITER $$
USE my_db $$
DROP PROCEDURE IF EXISTS my_sproc $$
CREATE PROCEDURE
my_sproc ()
BEGIN
   IF 1=1
   THEN
     SELECT 1;
   END IF;
END $$

If you do the same as above, but take out the delimiter statement and change all the '$$'s to ';'s, you'll get the same error again. It seems like it was getting confused somehow on the statements inside and outside the "IF" if they both expected the same delimiter.

 
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.