在MySQL中插入时出现语法错误

时间:2014-03-12 00:30:33

标签: mysql insert

我正在努力熟悉MySQL并感到沮丧。

我正在尝试创建一个函数......

use mystuff;
DELIMITER $$
create PROCEDURE spUpdateCodeTable(
   IN mcodeID INT,
   IN mtableCode CHAR(5), 
   IN mDescription VARCHAR(100), 
   IN mCode CHAR(5), 
   IN mgroupCode CHAR(5), 
   IN mt1 VARCHAR(100), 
   IN mt2 VARCHAR(100), 
   IN mt3 VARCHAR(100))
BEGIN
   UPDATE codeTable SET 
      tableCode =mtableCode,
      Description=mDescription,
      Code=mCode,
      groupCode=mgroupCode,
      t1=mt1,
      t2=mt2,
      t3=mt3,
      chaDate=NOW()
   WHERE codeID = mcodeID;

   IF (ROW_COUNT() < 1)
   BEGIN
      INSERT INTO codeTable (tableCode,Description,Code,groupCode,t1,t2,t3,createDate)
      VALUES (mtableCode,mDescription,mCode,mgroupCode,mt1,mt2,mt3,NOW());
   END;      
END$$
DELIMITER ;

当我只有UPDATE部分时,程序编译得很好,但是一旦我添加插入四行(从IF(ROW_COUNT()....开始),我开始得到......

#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 'BEGIN INSERT INTO codeTable (tableCode, Description, Code, groupCode,t1,t2,t' at line 23'

1 个答案:

答案 0 :(得分:0)

像@Phil说的那样,你会想要......

IF (ROW_COUNT() < 1) THEN
  INSERT INTO codeTable (tableCode,Description,CODE,groupCode,t1,t2,t3,createDate)
  VALUES (mtableCode,mDescription,mCode,mgroupCode,mt1,mt2,mt3,NOW());
END IF;
而不是你拥有的。

存储过程的MySQL错误消息不是很棒吗?