我在第9行不断收到语法错误
CREATE PROCEDURE `ItemsAddOrEdit`(
_itm_id INT,
_itm_name VARCHAR(255),
_itm_price FLOAT(8,2)
)
BEGIN
IF _itm_id = 0 THEN
INSERT INTO items (itm_name, itm_price)
VALUES (_itm_name, _itm_price);
ELSE
UPDATE items
SET
itm_name = _itm_name,
itm_price = _itm_price
WHERE itm_id = _itm_id;
END IF;
END
变量是问题吗?我检查了一下桌子,看看是否把名字弄乱了,但对我来说似乎一切都很好。 这是表格代码
CREATE TABLE `items` (
`itm_id` INT(255) NOT NULL AUTO_INCREMENT,
`itm_name` VARCHAR(255) NOT NULL,
`itm_price` FLOAT(8,2) NOT NULL,
PRIMARY KEY (`itm_id`),
UNIQUE INDEX `itm_name` (`itm_name`)
)
答案 0 :(得分:3)
Delimiter
重新定义为其他名称,例如:$$
。这使解析器可以忽略;
(因此到达;
时不执行语句)。DROP PROCEDURE IF EXISTS
,以防万一已经存在具有相同名称的过程时失败。;
尝试以下操作:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ItemsAddOrEdit` $$
CREATE PROCEDURE `ItemsAddOrEdit`(
_itm_id INT,
_itm_name VARCHAR(255),
_itm_price FLOAT(8,2)
)
BEGIN
IF _itm_id = 0 THEN
INSERT INTO items (itm_name, itm_price)
VALUES (_itm_name, _itm_price);
ELSE
UPDATE items
SET
itm_name = _itm_name,
itm_price = _itm_price
WHERE itm_id = _itm_id;
END IF;
END $$
DELIMITER ;