2010-04-12 29 views
0

我用下面的MySQL查詢,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

這不是一個查詢,它是一個程序創建腳本 – lexu 2010-04-12 06:40:04

+0

@lexu亞其程序,但爲什麼我得到那個錯誤? – bala3569 2010-04-12 06:45:50

+0

只是爲了完整性 - 是MySQL內部SP所需的分號嗎? – Axarydax 2010-04-12 06:49:46

回答

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 
0

您錯過了'如果'中的'THEN'...

+0

@ziang它是一個if exists聲明.. – bala3569 2010-04-12 06:48:00

+0

@ziang加上然後不工作... – bala3569 2010-04-12 06:48:44

+0

你也需要結束if – zsong 2010-04-12 07:14:44

0

隨着分號和THEN,你是缺少END IF來終止IF語句。