MySQL MYSQL存储过程If语句问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1141996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:41:33  来源:igfitidea点击:

MYSQL Stored Procedures If statement Problem

mysqlstored-procedures

提问by systemsfault

I'm working with Mysql 5.1.28-rc on freebsd. I've just decided to use stored procedures in MYSQL and created a test procedure as below:

我正在 freebsd 上使用 Mysql 5.1.28-rc。我刚刚决定在 MYSQL 中使用存储过程并创建了一个测试过程,如下所示:

DELIMITER $$ 
DROP PROCEDURE IF EXISTS test $$
CREATE PROCEDURE test( IN test VARCHAR(22) )
BEGIN
 DECLARE count INT(11);
 SET count = (SELECT COUNT(*) FROM Test WHERE test_column = test );
 SELECT count;
 IF count = 0 THEN
  SET count = 1;
 ELSE
  SET count = 2;
 ENDIF;
END $$
DELIMITER;

This procedure works well without IF statement in it , but with the if statement it gives, 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 语句的情况下运行良好,但是有了它给出的 if 语句, 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'

How can I solve this issue? Where is the problem?

我该如何解决这个问题?问题出在哪儿?

回答by David M

ENDIFrequires a space in MySQL doesn't it? i.e. END IF

ENDIF在 MySQL 中需要一个空格不是吗?IEEND IF

回答by mona

Just need space in end ifin the stored procedure

只需要end if存储过程中的空间

回答by user2606556

Should not use variables like count or something. So please find the solution for this-

不应该使用像 count 之类的变量。因此,请为此找到解决方案-

DELIMITER $$ 
DROP PROCEDURE IF EXISTS test $$
CREATE PROCEDURE test(IN test VARCHAR(22) )
BEGIN
 DECLARE var_count INT;
 SET var_count = (SELECT COUNT(*) FROM test WHERE test.textfield = test);
 IF var_count = 0 THEN
  SET var_count = 1;
 ELSE SET var_count = 2;
 END IF;
 SELECT var_count;
END $$