我想要一个如下所示的 if 查询。
set @i=1;
set @j=2;
if @i < @j then select * from test limit 1;
end if;
我收到以下错误,
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 @i < @j then select * from test limit 1' at line 1
也在程序中尝试过这个,
DELIMITER $$
DROP PROCEDURE IF EXISTS test $$
create procedure test ()
set @i=1;
set @j=2;
if @i < @j then select * from test limit 1;
end if;
END $$
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 @i < @j then select * from test limit 1;
end if;
END' at line 1
谁能帮我解决这个问题吗?
最佳答案
您需要一个开始
:
DELIMITER $$
DROP PROCEDURE IF EXISTS test $$
create procedure test ()
begin
set @i=1;
set @j=2;
if @i < @j then
select * from test limit 1;
end if;
END $$
DELIMITER ;
你的第一个不起作用,因为 if
只允许在编程 block 中使用。事实上,这就是为什么你的版本也不起作用——如果没有 begin
,if
就不在编程 block 中。
关于MySQL - 如果查询返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46984327/