SQL If Statement / select into in a stored procedure

时间:2013-12-16 19:11:22

标签: mysql sql stored-procedures

我无法理解为什么以下SQL过程不会存储在我的数据库中并报告错误,我已经使用了很长时间并且完全感到困惑。

DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN

DECLARE brandexists INT DEFAULT 0;

SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;

IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;

END

我得到的错误是:

  

#1064 - 您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在'WHERE BrandName = inBrandName INTO brandexists附近使用正确的语法; IF brandexists = 1 THEN sele'在第6行

任何想法?

1 个答案:

答案 0 :(得分:2)

您缺少一个可供选择的表:

SELECT count(*)   WHERE BrandName = inBrandName INTO brandexists;
                ^---here (from some_table)