我使用以下mysql查询,
DELIMITER $$
DROP PROCEDURE IF EXISTS `allied`.`aboutus_delete`$$
CREATE DEFINER=`allied`@`%` PROCEDURE `aboutus_delete`(
IN p_Id int(11)
)
BEGIN
if exists( select aboutUsId
from aboutus
where aboutUsId=p_id
and isDeleted=0
)
update aboutus set isDeleted=1 where aboutUsId=p_id
else
select 'No record to delete'
END$$
DELIMITER ;
但是当我执行它时我得到了这个错误......
Error Code : 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
'update aboutus set isDeleted=1 where aboutUsId=p_id
else
select 'No record to' at line 6
修改
使用分号似乎不起作用,
if exists(select aboutUsId from aboutus where aboutUsId=p_id and
isDeleted=0) then
update aboutus set isDeleted=1 where aboutUsId=p_id;
else
select 'No record to delete';
答案 0 :(得分:2)
这是一个不同的问题:您可以稍微优化此过程。为什么一个查询会执行两次数据存储?只需将isDeleted属性设置为1,然后检查row_count值。
BEGIN
UPDATE aboutus SET isDeleted = 1 WHERE aboutUsId = p_id AND isDeleted = 0;
IF (SELECT row_count()) <= 0 THEN
SELECT 'No record to delete';
END IF;
END
答案 1 :(得分:0)
你错过了'IF'中的'THEN'......
答案 2 :(得分:0)
除了分号和THEN之外,您还缺少END IF来终止IF语句。