IF syntax error

I get a syntax error following the MySQL manual for syntax IF.

My request:

if 0=0 then select 'hello world'; end if;

Logically, this should choose 'hello world', but instead I get

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if (0=0) then select 'hello world'' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end if' at line 1
+4
source share
3 answers

Your request is only valid in the context of a stored procedure / function. See there for reference.

+5
source

using if such statements are valid only within the stored procedure or functions.

What you would probably like to use is if () , and then you can use:

select IF(0=0, 'hello world','');
+1
source

If statements are not supported in the shared SQL thread. It can only be used in functions or procedures. However, you can use it as follows

use my_db;
delimiter #
create procedure xyz()
 begin
   IF 0=0 then select 'hello world';
 end if;
end#
0
source

Source: https://habr.com/ru/post/1628534/


All Articles