【问题标题】:Using an if statement outside a stored procedure在存储过程之外使用 if 语句
【发布时间】:2026-01-12 13:30:01
【问题描述】:

我正在尝试执行此代码

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;

但我总是收到错误

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

select 'h'' at line 1 

为什么我的 if 会产生错误?

【问题讨论】:

  • 你遇到了什么错误?
  • 我得到的错误是#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
  • 您阅读过这方面的手册吗?你的目的是什么?

标签: mysql


【解决方案1】:

您需要将代码放在存储过程或函数中才能使用 IF 语句 https://*.com/a/12954385/5308054。在这里你可以看到修复它的功能请求https://bugs.mysql.com/bug.php?id=48777

可以在没有 IF 语句的情况下重写查询,但我认为这个问题也无法以这种方式回答。

【讨论】: