9

I am trying to execute this code

set @id = 0;
set @the_number = 0;
set @the_message = 0;
set @selected_message = 0;

SELECT id, dest_msisdn, text_message INTO @id, @the_number, @the_message FROM incoming_sms where service_id = 6015592000101762 AND job_status = 0 limit 1;

if(@the_message LIKE '%Bank%')then

select 'h';

end if;

but i keep getting an error on

if(@the_message LIKE '%Bank%')then

select 'h'' at line 1 

Why is my if producing an error?.

  • what error are you getting? – Harry Oct 14 '14 at 10:22
  • The error i am getting is `#1064 - 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(@the_message LIKE '%Bank%')then select 'h'' at line 1 ` –  Oct 14 '14 at 10:23
  • 1
    Have you read the manual about this? What is your purpose? – Hendry Tanaka Oct 14 '14 at 10:25

1 Answers1

8

You need to put your code inside of a strored procedure or function to use the IF statement https://stackoverflow.com/a/12954385/5308054. Here you can see a feature request to fix it https://bugs.mysql.com/bug.php?id=48777

It is possible to rewrite query without IF statements but I think this question is too to be answered in such way.

Community
  • 1
  • 1
Dzmitry Paliakou
  • 1,587
  • 19
  • 27