7

I'm getting a syntax error while following the MySQL guide for IF syntax.

My query is:

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

Logically, this should select '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
0

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

Comments

2

using if statements like this is valid only inside stored procedure or functions.

What you'd probably like to use is the if() function, and then you can use:

select IF(0=0, 'hello world','');

Comments

0

If statements are not supported in general SQL flow. It can only be used in functions or procedures. However you can use it in following way

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

2 Comments

why not just use the built in IF() function?
yeah it is possible but I have answered as per the context of question as I got it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.